[英]how to get checkbox values in form in jquery dialog?
我在jquery对话中有一个表单。 提交表单后,如何获取所选复选框的值?
$(document).ready(function(){
var $dialog = $('<div></div>')
.html('<form id="myform" action=""><input type="checkbox" id="completeCheck" name="completeCheck" value="" />Complete check<br /><input type="checkbox" name="view" value="Car" /> View report <br /><input type="checkbox" name="consist" value="" />Consistency check<br /><input type="checkbox" name="other" value="" />Other checks<br /><input type="checkbox" name="keyCheck" value="" />Key check<br /><input type="checkbox" name="compareCheck" value="" />Compare check<br /></form>')
.dialog({
autoOpen: false,
title: 'Data check',
buttons: {
"Submit Form": function() { $('form#myform').submit();},
"Cancel": function() {$(this).dialog("close");}
}
});
$('#createNew').click(function() {
$dialog.dialog('open');
// prevent the default action, e.g., following a link
return false;
});
$('form#myform').submit(function(){
$dialog.dialog('close');
});
});
工作演示 http://jsfiddle.net/9mZAJ/2/ 或 http://jsfiddle.net/9mZAJ/1/
$('#completeCheck').is(':checked')
将执行诀窍以及.each
循环复选框休息,请参阅演示和下面的代码。
这应该有所帮助
现在循环遍历所有复选框,您可以使用.each
和with is(":checked")
检查以多种方式执行此操作。 :)
码
$(document).ready(function(){
var $dialog = $('<div></div>')
.html('<form id="myform" action=""><input type="checkbox" id="completeCheck" name="completeCheck" value="" />Complete check<br /><input type="checkbox" name="view" value="Car" /> View report <br /><input type="checkbox" name="consist" value="" />Consistency check<br /><input type="checkbox" name="other" value="" />Other checks<br /><input type="checkbox" name="keyCheck" value="" />Key check<br /><input type="checkbox" name="compareCheck" value="" />Compare check<br /></form>')
.dialog({
autoOpen: false,
title: 'Data check',
buttons: {
"Submit Form": function() { $('form#myform').submit();},
"Cancel": function() {$(this).dialog("close");}
}
});
$('#createNew').click(function() {
$dialog.dialog('open');
// prevent the default action, e.g., following a link
return false;
});
$('form#myform').submit(function(){
alert('completeCheck checkobox is checked or not ==> ' + $('#completeCheck').is(':checked'));
$dialog.dialog('close');
});
});
这样循环
$(this).find('input[type="checkbox"]').each(function(){
alert($(this).is(':checked'));
})
图片
您可以在表单submit-callback中使用$(this).find(":checked")
获取所有选中的复选框。 然后你可以遍历它们并获得它们的值。
下面是一个示例,说明如何将已选中复选框的名称及其值存储在数组中,例如:
$('form#myform').submit(function(){
// Store names of the checkboxes and their values in an array
var values = [];
$(this).find(":checked").each(function () {
values[$(this).prop("name")] = $(this).val();
});
$dialog.dialog('close');
});
在您的提交功能中:
$(this).find(':checkbox').each(function(){
if(this.checked)
alert(this.name + ' is checked');
else
alert(this.name + ' is not checked');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.