我在根据一组复选框的值进行测试和检查时遇到问题。下面的示例显示了一些复选框,其中如果复选框的值高于选中的值,则应选中。如果该值等于或小于检查的值,则不应检查。
感谢大家的帮助!
<label class="checkbox"><input type="checkbox" name="a" value="5"> One</label>
<label class="checkbox"><input type="checkbox" name="a" value="5"> two</label>
<label class="checkbox"><input type="checkbox" name="a" value="15"> three</label>
<label class="checkbox"><input type="checkbox" name="a" value="25"> four</label>
JS
$('input[type="checkbox"]').change(function (e) {
var $value = $(this).val();
var $siblings = $('input[name="' + $(this).attr("name") + '"]').not(":checked");
$.each($siblings, function (i, input) {
console.log(input.value)
if ($value != input.value) {
$(this).prop('checked', true);
}
else if ($value > input.value) {
$(this).prop('checked', true);
}
else if ($value >= input.value) {
$(this).prop('checked', true);
}
else {
$(this).prop("checked", false);
}
})
});
最佳答案
你可以做到
$('input[type="checkbox"]').change(function (e) {
var $value = parseInt($(this).val());
var $siblings = $('input[name="' + $(this).attr("name") + '"]:checked').not($(this));
var isCheck = true;
$siblings.each(function (i) {
var a = $(this).val() || 0;
if ($value <= a) {
isCheck = false;
}
});
$(this).prop("checked", isCheck);
});
$('input[name="' + $(this).attr("name") + '"]:checked').not($(this))
抓取选中的内容不包括您点击的内容的框。
each()
循环只是设置检查标志,然后根据结果将单击的框设置为检查或不检查。这还允许您取消选中单击的框
关于jquery - 如何通过使用 jQuery 测试复选框的高值和低值来检查复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30109520/