即使表单包含在 if 语句中并且不满足条件,仍会提交表单吗?
我已经将整个验证代码绑定到 $('#post-button').click
。这个想法是,变量e_exit
设置为true
,然后表单中的数据经过一系列检查。如果它满足任何 if 语句的条件,则会显示一条包含该问题的消息,并且 e_exit
设置为 false
。但问题是,它没有正确完成,因为表单仍在提交(就像显示消息后四分之一秒)。
我在这里处理布尔值,所以类型转换肯定不是问题,我真的不明白发生了什么:
$('#post-button').click( function() {
var e_exit = true;
var notif_message = '<div class="notification red"><div class="cross"></div>';
var name = $('input[name=name]').val();
var url = $('input[name=url]').val();
var urlrgx = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/i;
var urltst = urlrgx.test(url);
if(name == '' || name == 'Enter a name for this offer') {
$('#notification').html(notif_message + "You need to enter a name for this offer name. Try to pick something that you'll easily remember when seen again.</div>");
e_exit = false;
}
if(!urltst) {
$('#notification').html(notif_message + "You need to enter a valid offer url prefixed with http:// for this offer.</div>");
e_exit = false;
}
var rotation_select_val = parseInt($('select[name=r_id]').val());
if(rotation_select_val != 0) {
var min_val = isNaN($('input[name=r_min]').val());
var max_val = isNaN($('input[name=r_max]').val());
if(min_val || max_val) {
$('#notification').html(notif_message + "You need to enter a numeric value for your rotation's min and max hits for this offer.</div>");
e_exit = false;
}
}
var geo_select_val = $('select[name=g_country\\[1\\]]').val();
if(geo_select_val != 0) {
var geo_url_val = $('input[name=g_url\\[1\\]]').val();
var geo_urltst = urlrgx.test(geo_url_val);
if(!geo_urltst) {
$('#notification').html(notif_message + "You need to enter a valid url prefixed with http:// for your geo-location targets.</div>");
e_exit = false;
}
}
if(e_exit) {
$('form').submit();
}
});
任何关于为什么会发生这种情况的答案,或者带有解释的编辑都会有很大的帮助!
I've got an entire validation code bound to $('#post-button').click
. The idea is, the variable e_exit
is set to true
, then the data within the form goes through a series of checks. If it meets the condition of any if statement, a message is displayed with the issue, and e_exit
is set to false
. Except the problem is, that it isn't done properly, as the form is still submitted (like a quarter of a second after the message is displayed).
I'm dealing with boolean values here, so typecasting certainly can't be the issue, I really don't understand what's going on:
$('#post-button').click( function() {
var e_exit = true;
var notif_message = '<div class="notification red"><div class="cross"></div>';
var name = $('input[name=name]').val();
var url = $('input[name=url]').val();
var urlrgx = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/i;
var urltst = urlrgx.test(url);
if(name == '' || name == 'Enter a name for this offer') {
$('#notification').html(notif_message + "You need to enter a name for this offer name. Try to pick something that you'll easily remember when seen again.</div>");
e_exit = false;
}
if(!urltst) {
$('#notification').html(notif_message + "You need to enter a valid offer url prefixed with http:// for this offer.</div>");
e_exit = false;
}
var rotation_select_val = parseInt($('select[name=r_id]').val());
if(rotation_select_val != 0) {
var min_val = isNaN($('input[name=r_min]').val());
var max_val = isNaN($('input[name=r_max]').val());
if(min_val || max_val) {
$('#notification').html(notif_message + "You need to enter a numeric value for your rotation's min and max hits for this offer.</div>");
e_exit = false;
}
}
var geo_select_val = $('select[name=g_country\\[1\\]]').val();
if(geo_select_val != 0) {
var geo_url_val = $('input[name=g_url\\[1\\]]').val();
var geo_urltst = urlrgx.test(geo_url_val);
if(!geo_urltst) {
$('#notification').html(notif_message + "You need to enter a valid url prefixed with http:// for your geo-location targets.</div>");
e_exit = false;
}
}
if(e_exit) {
$('form').submit();
}
});
Any answers as to why this could be happening, or edits with explanations would be of a great help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
#post-button
是提交按钮,则需要返回false
来停止表单提交。更好的解决方案是绑定到表单的
submit
事件:只有当您从事件处理程序返回
true
时,才允许表单提交。编辑:
也许ajax插件也订阅了post按钮的
click
事件?在这种情况下,我会返回 false 并确保停止事件进一步传播,如下所示:If
#post-button
is a submit button, you need to returnfalse
to stop form submission.A better solution would be to bind to the form's
submit
event:This will only allow the form to submit if you return
true
from the event handler.Edit:
Perhaps the ajax plugin is also subscribing to the
click
event of the post button? In that case, I'd return false and make sure to stop the event propagating further like so: