我想获取复选框的所有值并提醒它们在表单提交时被选中,
到目前为止,这是我尝试过的:
HTML
<form id="calform">
<input type="checkbox" value="one_name" />
<input type="checkbox" value="one_name1"/>
<input type="checkbox" value="one_name2"/>
<input type="submit" value="Submit" />
</form>
jQuery 脚本
$("#calform").submit(function(e){
// array that will store all the values for checked ones
var allVals = [];
$('input[type="checkbox"] :checked').each(function() {
// looping through each checkbox and storing values in array for checked ones.
allVals.push($(this).val());
});
alert(allVals);
e.preventDefault();
});
这里是JSFIDDLE
表单提交时警告框显示为空。
最佳答案
使用 $('input[type="checkbox"]:checked')
,注意 input[type="checkbox"]
和伪类:checked
:
$("#calform").submit(function (e) {
var allVals = [];
$('input[type="checkbox"]:checked').each(function () {
// removed the space ^
allVals.push($(this).val());
});
alert(allVals);
e.preventDefault();
});
关于jquery - 在表单提交时获取数组中选中的复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21397953/