如果确认为真,我尝试调用另一个 jQuery 函数,代码如下:
jQuery("#adminForm_1").submit(function () {
var empty = false;
jQuery(":input", "#adminForm_1").each(function () {
empty = (jQuery(this).val() == "") ? true : empty;
});
if (empty) {
if (confirm('You have not filled out all of the fields, do you wish to continue?')) {
jQuery("#adminForm_1").validationEngine({
ajaxSubmit: true,
ajaxSubmitFile: "/index.php?option=com_database&view=tripdetails&Itemid=12&client=1&task=save",
ajaxSubmitMessage: "Client Trip Details Saved",
inlineValidation: false,
success: false,
failure: function () {}
});
} else {
return false;
};
}
});
^^ 上面的代码不起作用,但你会看到我正在尝试做什么..
最佳答案
您需要阻止浏览器对表单的默认操作,即以传统方式将其提交到服务器。在提交处理程序末尾return false
,或者在开头放置e.preventDefault()
:
jQuery("#adminForm_1").submit(function (e) {
e.preventDefault();
...
或者:
jQuery("#adminForm_1").submit(function () {
var empty = false;
jQuery(":input", "#adminForm_1").each(function () {
empty = (jQuery(this).val() == "") ? true : empty;
});
if (empty) {
if (confirm('You have not filled out all of the fields, do you wish to continue?')) {
...
});
}
}
return false;
});
参见preventDefault
:
Prevents the browser from executing the default action. Use the method isDefaultPrevented to know whether this method was ever called (on that event object).
正如旁注那样,return false
与 preventDefault
具有相同的效果,而且它会阻止事件冒泡到父元素。 jQuery 实现这一点的机制位于 stopPropagation方法。换句话说,return false
= e.preventDefault + e.stopPropagation
关于jquery - 如果确认为 true,则调用另一个 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1469875/