2011-06-14 35 views
1

我有這樣的表,它也會從一個XML填充上按一下按鈕所有複選框上的按鈕取消選中使用jQuery

<table id="tableDg" 
    style="border:#2F5882 1px solid;width:100%;" cellspacing="1" cellpadding="1"> 

    <thead> 
    <tr id="trHead" style="color :#FFFFFF;background-color: #8EA4BB" > 
     <th></th> 
     <th width="2%"><input id="chkbHead" type='checkbox' /></th> 
     <th width="10%" align="center" spry:sort="name"><b>Name</b></th> 
     <th width="22%" align="center" spry:sort="host"><b>Host</b></th> 

    </tr> 
    </thead> 

    <tbody spry:repeat="pv1"> 
    <tr class="trOdd" 
    spry:if="({ds_RowNumber} % 2) != 0" onclick="ds1.setCurrentRow('{ds_RowID}');" 
     style="color :#2F5882;background-color: #FFFFFF"> 
     <td><input type="hidden" id="path" value="{path}"></input></td> 
     <td><input type="checkbox" id="chkbTest" class = "chkbCsm" ></input></td> 
     <td width="10%" align="center">&nbsp;&nbsp;<input type="hidden" id="nameText" readonly="true" value="{name}"><a href="#" class="aDg">{name}</a></input></td> 
     <td width="22%" align="center">&nbsp;&nbsp;<input type="hidden" id="nameText" readonly="true" value="{host}">{host}</input></td> 

    </tr> 

    <tr class="trEven" name="trEven" id="trEven" 
    spry:if="({ds_RowNumber} % 2) == 0" onclick="ds1.setCurrentRow('{ds_RowID}');" 
     style="color :#2F5882;background-color: #EDF1F5;"> 
     <td><input type="hidden" id="path" value="{path}"></input></td> 
     <td><input type="checkbox" class = "chkbCsm" ></input></td> 
     <td width="10%" align="center">&nbsp;&nbsp;<input type="hidden" id="nameText" readonly="true" value="{name}"><a href="#" class="aDg">{name}</a></input></td> 
     <td width="22%" align="center">&nbsp;&nbsp;<input type="hidden" id="nameText" readonly="true" value="{host}">{host}</input></td> 

    </tr> 
    </tbody> 
    </table> 

點擊我調用一個函數和函數中我做類似下面

$('#tableDg input:checkbox').removeAttr('checked'); 

但沒有運氣。請幫助!!我想取消按鈕的所有複選框單擊

+0

什麼的console.log($( '#tableDg輸入:複選框'));正在顯示? – 2011-06-14 11:12:26

+0

對不起我的錯誤。我在頁面的其他部分發生了一些錯誤。它工作正常。 – abi1964 2011-06-14 11:13:34

回答

2

試試這個:

$('#tableDg input:checkbox').attr('checked',false); 
+0

不適合我;即使我小寫'A'也不起作用。 – 2011-06-14 11:01:23

+0

可能與選擇器有關。我只複製了OP提供的選擇器,但考慮給複選框添加一個類並將其用作選擇器。 – 2011-06-14 11:07:45

+0

對不起,我的壞。 '$('#tableDg input:checkbox')。removeAttr('checked');'也可以。我在其他部分有一些錯誤:)所以它沒有得到反映 – abi1964 2011-06-14 11:10:49

0

我用這個代碼來切換複選框的狀態:你的情況

checked = false; 
$('#select_all').click(function() { 
    checked = !checked 
    $(this).children('input:checkbox').attr('checked', checked); 
}); 

嘗試設置「檢查」爲假。此外,在螢火蟲/鉻檢查的控制檯中嘗試相同。

2

我會用$('#tableDg input[type=checkbox]').removeAttr('checked');

其他注意事項: 因爲:複選框不能利用:複選框是一個jQuery擴展和不CSS規範的一部分,使用查詢本機DOM querySelectorAll()方法提供的性能提升。爲了在現代瀏覽器中獲得更好的性能,請改用[type =「checkbox」]。

但我認爲這個問題是不是在你使用的代碼,當您點擊按鈕

更好的提供執行腳本也似乎你的複選框沒有價值和name屬性,我建議你加他們,如果你在表單

也在1.6使用它們有一個.removeProp('checked'),但配備一記

注意:不要使用此方法來移除固有特性如選中,禁用或選中。這將完全刪除該屬性,一旦刪除,不能再添加到元素。使用.prop()將這些屬性設置爲false。

所以你的代碼應該是

$('#tableDg input[type=checkbox]').prop('checked', false);

0

差不多就是何塞·瓦倫特說,但你也可以只檢查選中的複選框,而不是全部。

$('#tableDg input:checked').attr('checked',false); 
+0

不工作在jsfiddle:http://jsfiddle.net/WBwtw/ – 2011-06-14 11:04:36

+0

我很抱歉,但我不得不不同意你。我在這裏上傳了一個測試,它正常工作,因爲它應該。 http://isqueel.com/uncheck_all.html – Pantelis 2011-06-14 11:11:24

0

你可能有表的形式來處理輸入,所以你可以使用這個:

$('.my-button').click(
    function(){ 
     $(this).parents('form').find('input:checkbox').removeAttr('checked'); 
    } 
)