2015-02-09 47 views
1

我已經創建了一組動態複選框,我需要的是當我選中複選框時,該組複選框的其餘部分需要取消選中它已經被選中。我創建了一個JS撥弄請檢查當該組的其中一個文本框將檢查時,取消選中一組複選框

http://jsfiddle.net/kuc2jarw/

我的代碼

$.ajax({ 
       type: "POST", 
       contentType: 'application/json;charset=utf-8', 
       dataType:'json', 
       url : 'getAllRequisitionIds', 
       data : JSON.stringify(), 
       success : function(data) { 
        // alert("success"); 
         if(data!=''){ 
         $.each(data, function(i, item) { 

tr = $('<tr/>'); 
tr.append($("<td />").html('<input type="checkbox" id="reqid'+i+'" class="reqid" value='+item+'>')); 
tr.append("<td>" + item + "</td>"); 


          $('#table_load').append(tr); 
         }); 


       }); 

回答

2

您可以使用委派change事件處理做

//register a change handler for the checkboxes with class reqid 
$('#table_load').on('change', '.reqid', function() { 
    if (this.checked) { 
     //select all elements with class reqid except current one and uncheck them 
     $('.reqid').not(this).prop('checked', false) 
    } 
}) 
+0

感謝重播我更新了我的JSFiddle,請檢查http://jsfiddle.net/kuc2jarw/2/ – Don 2015-02-09 07:36:44

相關問題