[英]How to force checkbox to behave like radiobutton, double loop
我在2 for循環中有復選框。 我想強迫他們表現得像無線電按鈕。 每個Q(第一個循環)應該只選擇一個選項。 我有一個代碼的和平,但它不起作用。 怎么改呢?
script type="text/javascript">
var classes = $('.myDiv').map(function () {
return $(this).attr('class');
}).get();
$.each(classes, function(i) {
$(classes[i]).change(function() {
var checked = $(this).is(':checked');
$(classes[i]).prop('checked',false);
if(checked[i]) {
$(this).prop('checked',true);
}
});
});
在復選框輸入中添加一個公共類,然后像下面一樣使用該類
$("input:checkbox").change(function() { $(this).siblings().prop('checked', false); $(this).prop('checked', true); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <input type="checkbox" value="1" /> <input type="checkbox" value="1" /> <input type="checkbox" value="1" /> </td> </tr> <tr> <td> <input type="checkbox" value="1" /> <input type="checkbox" value="1" /> <input type="checkbox" value="1" /> </td> </tr> </table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.