[英]Check all checboxes if they have the same value
這是我的html代碼:
<ul>
<li>
<input name="Services[]" class="form-control" value="Canbeprivatized" id="Canbeprivatized" type="checkbox"><label for="Canbeprivatized">Can be privatized </label>
</li>
<li>
<input name="Services[]" class="form-control" value="Englishspoken" id="Englishspoken" type="checkbox"><label for="Englishspoken">English spoken </label>
</li>
<li>
<input name="Services[]" class="form-control" value="Françaisparle" id="Françaisparle" type="checkbox"><label for="Françaisparle">Français parlé </label>
</li>
<li>
<input name="Services[]" class="form-control" value="Petsallowed" id="Petsallowed" type="checkbox"><label for="Petsallowed">Pets allowed </label>
</li>
<li>
<input name="Services[]" class="form-control" value="Piscine" id="Piscine" type="checkbox"><label for="Piscine">Piscine </label>
</li></ul>
jQuery代碼:
var tab = new Array();
tab = data.services.split(",");// data is values of Checkboxes inserted in create form, so now i need to show it because this is edit form
我的問題是如何檢查所有值等於選項卡中值的復選框。 需要您的幫助,謝謝
執行以下操作,在tab
迭代:
for (i = 0; i < tab.length; i++)
$('input[type=checkbox][value=' + tab[i] + ']').prop("checked", true);
您可以使用.filter()
查找其值存在於array tab
所有checkboxes
,然后使用.prop()
設置其checked
屬性
$(':checkbox').filter(function(){
return tab.indexOf($(this).val()) > -1;
}).prop('checked', true);
如果我正確理解這應該工作
var tab = 'Englishspoken,Petsallowed';
tab = tab.split(",");
var $input = $('input');
for (var i = 0; i < $input.length; i++){
for (var j = 0; j < tab.length; j++){
if ($input.eq(i).val() == tab[j]){
$input.eq(i).prop('checked', true);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.