我正在测试我要在我的网站上使用的代码。我希望在jquery代码中选择多个名称。但它似乎不起作用。
其思想是,人们只能选择两个复选框组中的一个,但它似乎只使用复选框-1组上的代码。
我的代码:
<input type="checkbox" name="checkbox-1" />
<input type="checkbox"name="checkbox-1" />
<input type="checkbox"name="checkbox-1" />
<input type="checkbox"name="checkbox-2" /><br>
<input type="checkbox"name="checkbox-2" />
<input type="checkbox"name="checkbox-2" />
<input type="checkbox"name="checkbox-2" />
<input type="checkbox"name="checkbox-2" />JavaScript:
$('input[name="checkbox-1"], input["name="checkbox-2"]').on('change', function() {
$('input[name="checkbox-1"], input["name="checkbox-2"]').not(this).prop('checked', false);
});小提琴手:http://jsfiddle.net/57tr36kv/2/
我试过没有输入前面的名字,但它仍然没有做它应该做的事情。
发布于 2019-01-15 09:35:15
您的问题在于选择器input["name="checkbox-2"] -在名称之前不应该有":
$(document).ready(function() {
$('input[name="checkbox-1"], input[name="checkbox-2"]').on('change', function() {
$('input[name="checkbox-1"], input[name="checkbox-2"]').not(this).prop('checked', false);
});
});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="checkbox" name="checkbox-1" />
<input type="checkbox" name="checkbox-1" />
<input type="checkbox" name="checkbox-1" />
<input type="checkbox" name="checkbox-2" /><br>
<input type="checkbox" name="checkbox-2" />
<input type="checkbox" name="checkbox-2" />
<input type="checkbox" name="checkbox-2" />
<input type="checkbox" name="checkbox-2" />
https://stackoverflow.com/questions/54195977
复制相似问题