简体   繁体   English

如果它的值在数组中,如何设置选中的复选框

[英]How set checked checkbox if it's value is in array

I have inputs:我有输入:

  <div id="my">
   <input type="checkbox" value="1" name="names[]">
   <input type="checkbox" value="2" name="names[]">
   <input type="checkbox" value="3" name="names[]">
   <input type="checkbox" value="4" name="names[]">
  </div>

And my javascript:还有我的 javascript:

initValues=[1,2,3];
$('#my').find(':checkbox[name="names[]"]').each(function () {
  $(this).prop("checked", ($.inArray($(this).val(), initValues)));
});

And now all my checkboxes are checked.现在我所有的复选框都被选中了。 How must I change my code to set checked for ckeckboxes which values are in initValues array?我必须如何更改我的代码以设置检查 ckeckboxes 哪些值在 initValues 数组中?

$.inArray returns the index, not boolean. $.inArray 返回索引,而不是布尔值。 Also, parseInt your value because its considered as string when you pick it up.另外, parseInt 你的值,因为当你拿起它时它被认为是字符串。

initValues=[1,2,3];
            $('#my').find(':checkbox[name="names[]"]').each(function () {
              $(this).prop("checked", $.inArray(parseInt($(this).val()), initValues) == -1 ? false : true );
            });

 let initValues = [1, 2, 3]; $('#my').find(':checkbox[name="names[]"]').each(function() { if (initValues.some(v => v == $(this).val())) { $(this).prop('checked', true); } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="my"> <input type="checkbox" value="1" name="names[]"> <input type="checkbox" value="2" name="names[]"> <input type="checkbox" value="3" name="names[]"> <input type="checkbox" value="4" name="names[]"> </div>

You need to turn the value back into a number so it compares with the array value.您需要将该值转回一个数字,以便与数组值进行比较。

$.inArray(+$(this).val(), initValues))

Revised Example:修改示例:

 initValues=[1,2,3]; $('#my').find(':checkbox[name="names[]"]').each(function () { $(this).prop("checked", ($.inArray(+$(this).val(), initValues)) != -1); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="my"> <input type="checkbox" value="1" name="names[]"> <input type="checkbox" value="2" name="names[]"> <input type="checkbox" value="3" name="names[]"> <input type="checkbox" value="4" name="names[]"> </div>

function printChecked(isChecked, value) { const newProjectStages = projectstage.filter((p) => p !== value);函数 printChecked(isChecked, value) { const newProjectStages = projectstage.filter((p) => p !== value);

if (isChecked) {
  newProjectStages.push(value);
}

Setprojectstage(newProjectStages);

var items = document.getElementsByName("tr");
var selectedItems = [];
for (var i = 0; i < items.length; i++) {
  if (items[i].type == "checkbox" && items[i].checked == true)
    selectedItems.push(items[i].value);
}
console.log("selected val", selectedItems);
Setselectedprostages(selectedItems);

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置复选框检查值是否为是(Django) - How to Set Checkbox Checked if Value Is Yes (Django) 如何设置基于一个数组中的值选中的复选框并将值添加到另一个数组(如果选中),否则使用angular js删除? - how to set checkbox checked based on value in one array and add value to another array if checked otherwise remove using angular js? 将选中的复选框值及其名称存储在二维数组中 - Store the checked checkbox value and it's name in a two dimensional array 有没有办法在数组中获取复选框的选中值? [阵营] - Is there a way to get checkbox's checked value in array? [React] 如果输入值=,则选中复选框 - Set checkbox to be checked if input value = 复选框值选中和未选中的数组 - array of checkbox value checked and unchecked 如果选中复选框,则在数组中传递复选框值 - Passing checkbox value in array if checkbox is checked 复选框的值已添加到数组,但未选中该复选框 - The value of the checkbox is added to the array but the checkbox is not checked 如何在js中的复选框数组上将选中的属性设置为false - How to set checked properties false on checkbox array in js 如何从复选框数组中检查至少一个复选框,并且复选框中的文本区域不为空白,且其值为“ other”? - How to check at least one checkbox is checked from the array of checkboxes and the text area is not blank on checkbox with value “other” is checked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM