如果确认为真,则调用另一个jQuery函数 [英] Calling another jQuery function if confirm is true

查看:136
本文介绍了如果确认为真,则调用另一个jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果confirm为true,我试图调用另一个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 )){

jQuery(#adminForm_1)。validationEngine({
ajaxSubmit:true,
ajaxSubmitFile:/index.php?option=com_database&view=tripdetails&Itemid=12&client=1&task=save,
ajaxSubmitMessage:已保存客户旅行详情,
inlineValidation:false,
success:false,
failure:function(){}
});

} else {
return false;
};

}

});

^^上述代码无效,但您会看到我想要的内容你需要防止浏览器在窗体上进行默认操作,这是将其提交给服务器的传统方式办法。在提交处理程序结束时,或者返回false ,或者在开始处放置 e.preventDefault() p>

  jQuery(#adminForm_1)。submit(function(e){
e.preventDefault();
...

或者:

<$ p jQuery(#adminForm_1)。submit(function(){

var empty = false;
jQuery(:input,#adminForm_1 ).each(function(){
empty =(jQuery(this).val()==)?true:empty;
});
if(empty){
if(confirm('您没有填写所有字段,是否希望继续?')){
...
});
}
}
返回false;
});

请参阅 preventDefault


防止浏览器执行
的默认操作。使用方法
isDefaultPrevented来知道
这个方法是否曾被调用过(在
事件对象上)。

至于旁注 return false preventDefault 具有相同的效果,它会停止冒泡事件到父元素。 jQuery的实现机制在 stopPropagation 方法中。换句话说,返回false = e.preventDefault + e.stopPagagation


I'm trying to call another jQuery function if confirm is true, here's the code:

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;
        };

    }

});

^^ the above code doesn't work, but you'll see what I'm trying to do..

解决方案

You need to prevent the browser's default action on the form, which is to submit it to the server the traditional way. Either return false at the end of your submit handler, or put e.preventDefault() at the beginning:

jQuery("#adminForm_1").submit(function (e) {
    e.preventDefault();
    ...

or:

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;
});

See 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).

As as side-note return false has the same effect as preventDefault plus it stops the bubbling of the event to parent elements. jQuery's mechanism for achieving that is in the stopPropagation method. In other words, return false = e.preventDefault + e.stopPropagation

这篇关于如果确认为真,则调用另一个jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆