简体   繁体   English

获取选定复选框的值和文本

[英]Get value and text of selected checkboxes

I am trying to get an array of selected checkboxes values and text on button click我正在尝试在单击按钮时获取一组选定的复选框值和文本

<input type="checkbox" name="checkbox[]" value="1" /><label>text1</label>
<input type="checkbox" name="checkbox[]" value="2" /><label>text2</label>
<input type="checkbox" name="checkbox[]" value="3" /><label>text3</label>
<input type="checkbox" name="checkbox[]" value="4" /><label>text4</label>

I'm getting values of the checkboxes but how to get text also in array我正在获取复选框的值,但如何获取数组中的文本

var checkboxes = document.getElementsByName('checkbox[]');
var vals = 0;
for (var i=0, n=checkboxes.length;i<n;i++) {
    if (checkboxes[i].checked) {
        vals += ","+checkboxes[i].value;
    }
}

Thank you!谢谢!

You can use map() to create an array of the values you require:您可以使用map()创建所需值的数组:

var labelValues = $(':checkbox:checked').map(function() {
    return [ $(this).next('label').text(), this.value ];
}).get();

However given your desired output it would make more sense to create an object with the labels as the properties, something like this:但是,鉴于您想要的输出,创建一个以标签作为属性的对象会更有意义,如下所示:

var obj = {};
$(':checkbox:checked').each(function() {
    obj[$(this).next('label').text()] = this.value;
});

-- 2021 Update -- -- 2021 更新 --

You can now simplify the map() call using an ES6 arrow function.您现在可以使用 ES6 箭头函数简化map()调用。 In addition you could combine the checkbox value and label text in to an array of objects, like this:此外,您可以将复选框值和标签文本组合到一个对象数组中,如下所示:

 $(':checkbox').on('change', () => { let labelValues = $(':checkbox:checked').map((i, el) => ({ value: el.value, text: el.nextElementSibling.textContent })).get(); console.log(labelValues); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" name="checkbox[]" value="1" /><label>text1</label> <input type="checkbox" name="checkbox[]" value="2" /><label>text2</label> <input type="checkbox" name="checkbox[]" value="3" /><label>text3</label> <input type="checkbox" name="checkbox[]" value="4" /><label>text4</label>

@Rory's answers is good to return both value and text into a single array. @Rory 的答案很好地将值和文本都返回到一个数组中。 But as the OP is asking to return value and text into separate arrays, I would propose the following way to do that.但是由于OP要求将值和文本返回到单独的数组中,我建议使用以下方法来做到这一点。

All you have to do is to use 2 arrays to get separate values from text.您所要做的就是使用 2 个数组从文本中获取单独的值。

Working JSFiddle here.在这里工作 JSFiddle。

Run the below snippet to see the output in the console.运行以下代码段以查看控制台中的输出。
This example will return:这个例子将返回:

  1. Selected checkbox Values in an array选定的复选框数组中的值
  2. Selected checkbox texts in an array数组中选定的复选框文本
  3. Selected checkbox in an array (In case if you want it)数组中的选定复选框(如果需要)

 var selectedCheckBoxValue = []; var selectedCheckBoxText = []; var selectedCheckBox = $(':checkbox:checked').map(function(i) { selectedCheckBoxValue[i] = this.value; selectedCheckBoxText[i] = $(this).next('label').text(); return this; }).get(); //Selected Checkboxes Value in a seperate Array console.log(selectedCheckBoxValue); //Selected Checkboxes Text in a seperate Array console.log(selectedCheckBoxText); //Selected Checkboxes element in a seperate Array console.log(selectedCheckBox);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <input type="checkbox" name="checkbox[]" value="1" checked="true" /> <label>text1</label> <input type="checkbox" name="checkbox[]" value="2" checked="true" /> <label>text2</label> <input type="checkbox" name="checkbox[]" value="3" /> <label>text3</label> <input type="checkbox" name="checkbox[]" value="4" checked="true" /> <label>text4</label>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM