2016-01-04 51 views
3

在CheckboxGroup做出选择我有一个<CheckboxGroup>如下:需要使用jQuery

<CheckboxGroup name="permitted"> 
    <input type="checkbox" value="1" />Orders 
    <input type="checkbox" value="2" />Production 
    <input type="checkbox" value="3" />Dispatch 
    <input type="checkbox" value="4" />Returns 
    <input type="checkbox" value="5" />Sundry 
    <input type="checkbox" value="6" />Collection 
    <input type="checkbox" value="7" />Pending Amount 
    <input type="checkbox" value="8" />Pending Bills 
    <input type="checkbox" value="9" />Ledger 
    <input type="checkbox" value="10" />Day Book 
</CheckboxGroup> 

我试图做一些挑选出来的这个复选框,

想我需要的订单,调度使用jQuery进行检查。 我对我有相应的值1,3。

检查我的小提琴:FIDDLE

回答

3

可以是那些使用属性选择器选择和使用.prop()检查复选框:

$('[value="1"], [value="3"]').prop("checked", true); 

$('[value="1"], [value="3"]').prop("checked", true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="checkbox" value="1" />Orders 
 
<input type="checkbox" value="2" />Production 
 
<input type="checkbox" value="3" />Dispatch 
 
<input type="checkbox" value="4" />Returns 
 
<input type="checkbox" value="5" />Sundry 
 
<input type="checkbox" value="6" />Collection 
 
<input type="checkbox" value="7" />Pending Amount 
 
<input type="checkbox" value="8" />Pending Bills 
 
<input type="checkbox" value="9" />Ledger 
 
<input type="checkbox" value="10" />Day Book

更新

从AJAX,你可以这样说:

$(function() { 
 
    valuesFromAjax = "1,2"; 
 
    valuesFromAjax = valuesFromAjax.split(","); 
 
    valuesFromAjax.map(function (Id) { 
 
    $('[value="' + Id + '"]').prop("checked", true); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="checkbox" value="1" />Orders 
 
<input type="checkbox" value="2" />Production 
 
<input type="checkbox" value="3" />Dispatch 
 
<input type="checkbox" value="4" />Returns 
 
<input type="checkbox" value="5" />Sundry 
 
<input type="checkbox" value="6" />Collection 
 
<input type="checkbox" value="7" />Pending Amount 
 
<input type="checkbox" value="8" />Pending Bills 
 
<input type="checkbox" value="9" />Ledger 
 
<input type="checkbox" value="10" />Day Book

+0

谢谢哥们。我还有一个疑问,我从ajax获得了1,2个,还有其他方面,以便我可以直接通过这个? – Santhucool

+0

@Santhucool你怎么得到1,2?作为一个字符串? –

+0

是的好友,你是现货! Yah它正在作为一个字符串 – Santhucool

2

假设数组有字符串值,可以使用下面的代码:

$(document).ready(function() { 
 
    //init an array with the values from OP 
 
    var values = ["1", "3"]; 
 
    //iterate through each checkbox 
 
    $("CheckboxGroup[name='permitted'] :checkbox").each(function() { 
 
    //if value exist in array change the status of the checkbox 
 
    $(this).prop("checked", values.indexOf(this.value) !== -1); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<CheckboxGroup name="permitted"> 
 
    <input type="checkbox" value="1" />Orders 
 
    <input type="checkbox" value="2" />Production 
 
    <input type="checkbox" value="3" />Dispatch 
 
    <input type="checkbox" value="4" />Returns 
 
    <input type="checkbox" value="5" />Sundry 
 
    <input type="checkbox" value="6" />Collection 
 
    <input type="checkbox" value="7" />Pending Amount 
 
    <input type="checkbox" value="8" />Pending Bills 
 
    <input type="checkbox" value="9" />Ledger 
 
    <input type="checkbox" value="10" />Day Book 
 
</CheckboxGroup>