2013-08-21 21 views
0

我有一個函數,它遍歷一個表並檢查<td>中的一些複選框,現在可以多次執行這些複選框,其中選擇的複選框不同。但我的問題是,如果我在一個單一實例執行兩次步,然後選擇工作不正常,該表是jQuery複選框 - 多次迭代不工作

<div> 
<table> </table> 
</div> 

代碼中的複選框

// global 
var selectedId ="" 


function populate(Id){ 


if(selectedId != Id){ 

    selectedId = Id; 

    // first uncheck previous selection if any 

    // selective is the class of check box <td> 
    // <td class="selective"><input type="checkbox" name="packId" 
    // value="${pack.packId}"></td> 

    $('.selective input:checkbox').each(function() { 
     var prevCheckedVal = (this.checked ? $(this).val() :""); 
     if(prevCheckedVal != ""){ 
     $(this).find("input[type=checkbox]").attr("checked", false); 
     } 
    }); 

    // now select check boxes for present selection 
    $("tr.allVPClass").each(function() { 
     $this = $(this) 
     var catId = $this.find("input.IdInVpClass").val(); 
     if(selectedId == catId){ 
     $(this).find("input[type=checkbox]").attr("checked", true); 
     } 
    }); 
} 
// open the dialoge 
$("#dialog-form").dialog("open"); 
} 

填充這個表在多個div。如果我打開然後單獨DIV其工作,但不選擇/檢查的任何複選框,如果我打開一個相同的div兩次

+0

不清楚你想用你的代碼完成什麼,也應該解釋一些你正在遇到的問題,你是什麼意思「打開單獨的div」? –

回答

0

爲了改變你需要使用.prop()而不是檢查狀態.attr()

$(this).find("input[type=checkbox]").prop("checked", false); 

還可以閱讀thisprop vs attr

0

您應該使用.prop()而不是.attr()的複選框

$(this).find("input[type=checkbox]").prop("checked", true); 
+0

謝謝,它使用道具後工作 – user2190050