如果在数组中签入一个复选框,我如何禁用或取消选中所有复选框?

时间:2015-06-09 12:27:48

标签: javascript jquery checkbox

我必须在项目中使用一组复选框。

这就是我的HTML的样子:

<label class="checkbox-inline">
  <input type="checkbox" name="hairColor" value="1"> hairColor-1
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="hairColor" value="2"> hairColor-2
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="hairColor" value="3"> hairColor-3
</label>        
<label class="checkbox-inline">
  <input type="checkbox" name="hairColor" value="4" class="any"> any
</label>

<br> 

<label class="checkbox-inline">
  <input type="checkbox" name="eyeColor" value="1"> eyeColor-1
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="eyeColor" value="2"> eyeColor-2
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="eyeColor" value="3"> eyeColor-3
</label>        
<label class="checkbox-inline">
  <input type="checkbox" name="eyeColor" value="4" class="any"> any
</label>

使用此复选框,用户可以选择他们的首选选项。但是如果他们用&#34;任何&#34;选择复选框文本。我需要取消选中已在组内进行的所有其他选择。而且,如果他们检查该复选框,则不应检查其他chechbox。

这是我使用Jquery尝试的方法。

$( "body" ).on( "change", ".checkbox-inline > input[type=checkbox]", function() {
    if ($(this).hasClass("any")) { //Any checkbox tick
        if ($(this).prop("checked")) { //User checked any
            //Other checkboxes are unchecked
            $(".checkbox-inline > input[type=checkbox][name=" + $(this).attr("name") + "]:not(.any)").prop("checked", false)
        }
    } else { //Other checkbox tick
        if ($(this).prop("checked")) {//User checked another checkbox
            //Any checkbox is unchecked
            $("[name=" + $(this).attr("name") + "].any").prop("checked", false);
        }
    }
});

它为我工作。但我的问题是当我将name值更改为array类型hairColor[]时,我的代码无效。

2 个答案:

答案 0 :(得分:2)

我喜欢AmmarCSE的答案,我赞成了它,但我相信肯定还有改进的余地。您的name属性中可能包含其他字符,并且您可能有更多字符实例。因此,我认为一个简单的函数应该可以解决这个问题,如下所示:

function escapeSpecialChars(input) {
     return input.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

然后你应用它,就像这样:

$( "body" ).on( "change", ".checkbox-inline > input[type=checkbox]", function() {
    var escapedSelector = escapeSpecialChars($(this).attr("name"));
    if ($(this).hasClass("any")) { //Any checkbox tick
        if ($(this).prop("checked")) { //User checked any
            //Other checkboxes are unchecked
            $(".checkbox-inline > input[type=checkbox][name=" + escapedSelector + "]:not(.any)").prop("checked", false)
        }
    } else { //Other checkbox tick
        if ($(this).prop("checked")) {//User checked another checkbox
            //Any checkbox is unchecked
            $("[name=" + escapedSelector + "].any").prop("checked", false);
        }
    }
});

Working fiddle

答案 1 :(得分:1)

由于您的名称属性包含[],您需要转义它们或切换单/双引号用法,例如

$('[name="' + $(this).attr("name") + '"].any')

$("body").on("change", ".checkbox-inline > input[type=checkbox]", function() {
  if ($(this).hasClass("any")) { //Any checkbox tick
    if ($(this).prop("checked")) { //User checked any
      //Other checkboxes are unchecked
      $('.checkbox-inline > input[type=checkbox][name="' + $(this).attr("name") + '"]:not(.any)').prop("checked", false)
    }
  } else { //Other checkbox tick
    if ($(this).prop("checked")) { //User checked another checkbox
      //Any checkbox is unchecked
      $('[name="' + $(this).attr("name") + '"].any').prop("checked", false);
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<label class="checkbox-inline">
  <input type="checkbox" name="hairColor[]" value="1">hairColor-1
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="hairColor[]" value="2">hairColor-2
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="hairColor[]" value="3">hairColor-3
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="hairColor[]" value="4" class="any">any
</label>

<br>

<label class="checkbox-inline">
  <input type="checkbox" name="eyeColor[]" value="1">eyeColor-1
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="eyeColor[]" value="2">eyeColor-2
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="eyeColor[]" value="3">eyeColor-3
</label>
<label class="checkbox-inline">
  <input type="checkbox" name="eyeColor[]" value="4" class="any">any
</label>

请参阅jQuery selector for inputs with square brackets in the name attribute

相关问题