有一个有 4 列的表。每列都有一些单选按钮。用户只能从每一列中选择一个单选按钮。由于表中有 4 列,因此用户可以从此表中选择 4 个单选按钮。但我想限制单选按钮的选择。我希望用户只能从该表中选择 4 个单选按钮中的 3 个。如果用户想选择数字 4,则会出现警告“您不能选择超过三个”。
这里可以设置限制吗?我该如何设置这个限制?
最佳答案
$("input[type='radio']").change(function(){
var count = $("input[type='radio']:checked").length;
if(count>3){
$(this).prop('checked', false);
alert("You cannot add more than 3");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="radioName1" value="1"> 1 <br>
<input type="radio" name="radioName1" value="2"> 2 <br>
<input type="radio" name="radioName1" value="3"> 3 <br>
<hr>
<input type="radio" name="radioName2" value="1"> 1 <br>
<input type="radio" name="radioName2" value="2"> 2 <br>
<input type="radio" name="radioName2" value="3"> 3 <br>
<hr>
<input type="radio" name="radioName3" value="1"> 1 <br>
<input type="radio" name="radioName3" value="2"> 2 <br>
<input type="radio" name="radioName3" value="3"> 3 <br>
<hr>
<input type="radio" name="radioName4" value="1"> 1 <br>
<input type="radio" name="radioName4" value="2"> 2 <br>
<input type="radio" name="radioName4" value="3"> 3 <br>
关于javascript - 如何设置表格中单选按钮选择的限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39890089/