我想获取复选框的所有值,并提醒在表单提交时检查它们,
这是我到目前为止所尝试的:
HTML
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(); });