2013-03-31 28 views
2

我想只檢查具有特定值的複選框。 我知道如何讓價值,我知道如何檢查所有的複選框,但我無法弄清楚如何檢查只用的,可以說的「200」只選中具有特定值的複選框

我的示例代碼值:

<tr> 
    <td><input class="chk_box" type="checkbox" value="200" /></td> 
    <td class="code">200</td> 
</tr> 
<tr> 
    <td><input class="chk_box" type="checkbox" value="0" /></td> 
    <td class="code">0</td> 
</tr> 
<tr> 
    <td><input class="chk_box" type="checkbox" value="0" /></td> 
    <td class="code">0</td> 
</tr> 
<tr> 
    <td><input class="chk_box" type="checkbox" value="200" /></td> 
    <td class="code">200</td> 
</tr> 
<tr> 
    <td><input class="chk_box" type="checkbox" value="300" /></td> 
    <td class="code">300</td> 
</tr> 

<input type="checkbox" class="checkbox_200" label="check 200" />check 200 

所以現在我需要jQuery代碼來完成我的代碼。 我感謝任何幫助。

順便說一句。有沒有辦法一次檢查'200'和'300'?

已更新!

+0

_your_代碼在哪裏? – moonwave99

+0

這是一個好點,我沒有chk_box值。我編輯這段代碼的時間太長了,我錯過了。 – Chriser

回答

4

您的複選框沒有value屬性,如果您想根據TD元素的文本內容勾選複選框,可以使用filter方法。

$('td.code').filter(function(){ 
    var txt = this.textContent || this.innerText; 
    return txt === '200' || txt === '300'; 
}).prev().find('input').prop('checked', true); 

更新

$('tr input[type=checkbox][value=200]').prop('checked', true); 

備選:

$('tr input[type=checkbox]').filter(function(){ 
    return this.value === '200' || this.value === '300'; 
}).prop('checked', true); 
+0

太棒了,像魅力一樣工作。謝謝 – Chriser

+0

有沒有辦法檢查,取消選項? – Chriser

+0

@Chriser是的,在'prop'方法中使用'false'而不是'true'。 – undefined

4

您的複選框也沒有價值。下一個只有文本。 嘗試更改代碼併爲輸入設置值。

<input class="chk_box" type="checkbox" value="200" /> 200 
<br /> 
<input class="chk_box" type="checkbox" value="0" />0 
<br /> 
<input class="chk_box" type="checkbox" value="0" />0 
<br /> 
<input class="chk_box" type="checkbox" value="200" /> 200 
<br /> 
<input class="chk_box" type="checkbox" value="300" /> 300 

的,你可以使用這個選擇:

input[type=checkbox][value="200"] 

jQuery的使用它裏面$( 「」)。

例子: http://jsbin.com/oyetit/1/edit

+0

謝謝你,我剛剛搞錯了最初的代碼,現在更新了。 – Chriser

+0

一次選擇200&300使用: $('input [type = checkbox] [value =「200」],input [type = checkbox] [value =「300」]')。prop(「checked 「,真正); – Saram

0

好吧,我結束了這個解決方案,略作修改,並感謝@undefined:

var chk = $(this).attr('checked')?true:false; 
$('tr input[type=checkbox][value=200]').attr('checked',chk);  

這讓我檢查/取消選中複選框與價值觀我很感興趣在

與同爲多個值:

var chk = $(this).attr('checked')?true:false; 
$('tr input[type=checkbox]').filter(function(){ 
    return this.value === '200' || this.value === '300'; 
}).attr('checked',chk); 
相關問題