javascript - 当选中复选框时,禁用其他相同值的复选框

标签 javascript jquery

如何在选中一个复选框时禁用其他相同值的复选框。

<input type='checkbox' name='check[]' value = '1'>
<input type='checkbox' name='check[]' value = '2'>
<input type='checkbox' name='check[]' value = '1'>
<input type='checkbox' name='check[]' value = '2'>

最佳答案

使用 change()事件处理程序并根据检查的属性禁用其他元素。您可以使用 filter() 获取具有相同值的其他元素。方法和attribute equals selector .

// cache the elements
var $chk = $("[name='check[]']");
// bind change event handler
$chk.change(function() {
  $chk
    // remove current element
    .not(this)
    // filter out element with same value
    .filter('[value="' + this.value + '"]')
    // update disabled property based on the checked property
    .prop('disabled', this.checked);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' name='check[]' value='1'>
<input type='checkbox' name='check[]' value='2'>
<input type='checkbox' name='check[]' value='1'>
<input type='checkbox' name='check[]' value='2'>

关于javascript - 当选中复选框时,禁用其他相同值的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41056093/

相关文章:

javascript - 如何使用 getElementsByClass 更改当前编辑的类?

php - 简单的 PHP 无法运行

javascript - 在数组中过滤数组

javascript - $http ajax AngularJS POST 在 PHP 中返回 undefined index

jQuery : Replacing Fading with Sliding

javascript - 停止按钮获得焦点

javascript - 删除具有所述 child ID 的 child 的 child

javascript - 添加ajax成功返回html形式的json对象数据

javascript - 动态创建的 HTML 表格行无法正常工作

jquery - 可拖动的 SimpleModal 弹出窗口