【发布时间】:2017-03-22 09:34:45
【问题描述】:
我正在使用jQuery对话框进行确认,无论是Submit表单还是Not
在提交表单之前我正在做一些验证。当我单击提交按钮时,它首先提醒 End,它位于提交按钮单击处理程序的末尾,并且我的验证失败
以下是我的代码 sn-p
$('#id-submit-btn').click( function( event ) {
//var $this = $(this);
var submit = true;
if( !$('.class-employees-cb:checked').length ) {
$("<div title='Employee Selection'>Please select at least one employee.</div>").dialog({
buttons: [ { text: 'OK', click: function() { $(this).dialog('close'); } } ]
});
submit = false;
//event.preventDefault();
//alert("2");
}
if( $('input:checkbox[name=isPrepondPostpond]').is(':checked') ) {
//event.preventDefault();
$("<div title='Prepond/Postpond Alert'>Have You Changed Meeting Date before Prepond / Postpond ?</div>").dialog({
buttons:[
{
text: 'Yes',
click: function() {
$(this).dialog('close');
$('#id-submit-btn').submit();
}
},
{
text: 'No',
click: function() {
submit = false;
//event.preventDefault();
$(this).dialog('close');
}
}
]
});
//alert("5");
}
alert("End");
//event.preventDefault();
if( !submit )
return false;
});
我想阻止表单提交如果用户点击否,如何实现?
【问题讨论】:
-
请使用
<>按钮创建minimal reproducible example。return submit也足够了。但是,我强烈建议使用表单的提交事件并在出现错误时使用 preventDefault。也不要在表单中调用任何“提交” -
我会尽力让你知道
-
我把
$('#id-submit-btn').click(function( event ) {换成了$('#id-submit-form').submit( function( event ) {,但是它不等我点击对话框的按钮,直接提交表单,如果验证失败也取消注释event.preventDefault(); -
$('#id-submit-form').on("submit", function( event ) {event.preventDefault(); .... }) -
还有 `$('#id-submit-form').submit();` 在确定按钮上
标签: jquery forms jquery-ui-dialog