0

嗯,我正在测试一些我将在我的网站中使用的代码。我想在我的 jquery 代码中选择多个名称。但它似乎不起作用。

这个想法是人们只能选择两个复选框组中的 1 个,但它似乎只使用了 checkbox-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);
});

jsfiddle:http: //jsfiddle.net/57tr36kv/2/

我试过在名字前面不输入 INPUT,但它仍然没有做它应该做的事情。

4

1 回答 1

2

您的问题在于选择器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" />

于 2019-01-15T09:35:15.960 回答