我试图只检查一个复选框,而其他人取消选中,但是当我想检查新的chekcbox时,那么pervious将取消选中,依此类推.
我试过这段代码:
JS文件
$('input[type="checkbox"]').click(function() {
$(".check_class").attr("checked", false);
$(this).attr("checked", true);
});
Run Code Online (Sandbox Code Playgroud)
HTML文件
<ons-list-header>Takistused</ons-list-header>
<ons-list-item modifier="tappable">
<label class="checkbox checkbox--noborder checkbox--list-item">
<input type="checkbox" id="example">
<div class="checkbox__checkmark checkbox--noborder__checkmark checkbox--list-item__checkmark"></div>
vihmaveerenn
</label>
</ons-list-item>
<ons-list-item modifier="tappable">
<label class="checkbox checkbox--noborder checkbox--list-item" id="test">
<input type="checkbox" id="example">
<div class="checkbox__checkmark checkbox--noborder__checkmark checkbox--list-item__checkmark" ></div>
äärekivi
</label>
</ons-list-item>
<ons-list-item modifier="tappable" >
<label class="checkbox checkbox--noborder checkbox--list-item" id="test1">
<input type="checkbox" id="example">
<div class="checkbox__checkmark checkbox--noborder__checkmark checkbox--list-item__checkmark"></div>
munakivi tee
</label>
</ons-list-item>
</ons-list>
Run Code Online (Sandbox Code Playgroud)
我做错了什么?我尝试了不同的方法,但它不会在现场使用.
试试.not(this)如下:
$(document).on('click', 'input[type="checkbox"]', function() {
$('input[type="checkbox"]').not(this).prop('checked', false);
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" id="one" />
<input type="checkbox" id="two" />
<input type="checkbox" id="three" />
<input type="checkbox" id="four" />Run Code Online (Sandbox Code Playgroud)
您确实应该使用单选按钮来实现此类功能,但是您可以简单地从一组匹配中排除某个项目,not如下所示:
$('input[type="checkbox"]').click(function() {
$('input[type="checkbox"]').not(this).prop("checked", false);
});
Run Code Online (Sandbox Code Playgroud)
JSFiddle: http://jsfiddle.net/TrueBlueAussie/4n2e31oq/
请注意,您需要使用propand 而不是attr某些属性,例如checked.
您可以通过缓存复选框的选择来提高效率:
var $checks = $('input[type="checkbox"]');
$checks.click(function() {
$checks.not(this).prop("checked", false);
});
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/TrueBlueAussie/4n2e31oq/1/
| 归档时间: |
|
| 查看次数: |
6873 次 |
| 最近记录: |