如果 $.get 返回值,则单击警报,如果没有,则提交表单

发布于 2024-08-31 17:24:51 字数 3250 浏览 4 评论 0原文

如果单击提交按钮,则阻止默认操作并查看字段“account_name”是否已在使用中。如果 $.get() 返回结果,则提醒用户该结果。如果没有,请使用 id="add_account_form" 提交表单。

我的问题是我的 else{} 语句没有提交表单。单击“提交”后,我没有得到任何响应;没有返回值。

另外,我想更改我的代码 $("#add_account_form").submit(..) 而不是 .click() 但是,这会导致稍后在脚本中尝试提交表单时出现问题?

    <script type="text/javascript">
        $(document).ready( function () { 

            $("#submit").click( function () { 
                 var account_name = $("input[name=account_name]").val();
                    $.get(
                        "'.url::site("ajax/check_account_name").'", 
                    {account_name: account_name}, 
                    function(data){ 
                        if(data.length > 0){            
                                    confirm( "The account name you entered looks like the following:\n"
                                                        +data+
                                            "Press cancel if this account already exists or ok to create it."
                                                ); 
                        }else{
                            $("#add_account_form").submit();
                        }                   
                    });
                    return false;                
                });
            });
    </script>
                                                    <p>
                                                        <input type="submit" id="submit" class="submit small" name="submit" value="Submit" />
                                                    </p>
                                                </form> 

感谢您的帮助。

编辑

所以任何遇到我的问题的人都会发现 $.get() 是异步的,所以它总是返回 false,或者 true,具体取决于 SubmitForm 的定义。然而,$.ajax() 允许将 async 设置为 false,这允许函数在继续之前完成。明白我的意思:

        $(document).ready( function () {                                
            $("#add_account_form").submit( function () { 
                var submitForm = true;
                 var account_name = $("input[name=account_name]").val();
                    $.ajax({
                        type: "GET",
                        async: false,
                        url: "'.url::site("ajax/check_account_name").'", 
                        data: ({account_name: account_name}), 
                        success:
                    function(data){ 
                        if(data.length > 0){            
                                    if(!confirm( "The account name you entered looks like the following:\n"
                                                        +data+
                                                        "Press cancel if this account already exists or ok to create it."
                                                )){ 
                                                submitForm = false;
                                            }
                        }   
                    }
                    });

                        if (submitForm == false ) {
                    return false;     
                        }

                });
            });

感谢您的帮助@Dan

If the submit button is clicked, prevent the default action and see if the field 'account_name' is already in use. If the $.get() returns a result, alert the user of the results. If it doesn't, submit form with id="add_account_form".

My problem is that my else{} statement is not submitting the form. I get no response when submit is clicked & there is no value returned.

Also I would like to change my code where it goes $("#add_account_form").submit(..) instead of .click() however, would that cause a problem when trying to submit the form later in the script?

    <script type="text/javascript">
        $(document).ready( function () { 

            $("#submit").click( function () { 
                 var account_name = $("input[name=account_name]").val();
                    $.get(
                        "'.url::site("ajax/check_account_name").'", 
                    {account_name: account_name}, 
                    function(data){ 
                        if(data.length > 0){            
                                    confirm( "The account name you entered looks like the following:\n"
                                                        +data+
                                            "Press cancel if this account already exists or ok to create it."
                                                ); 
                        }else{
                            $("#add_account_form").submit();
                        }                   
                    });
                    return false;                
                });
            });
    </script>
                                                    <p>
                                                        <input type="submit" id="submit" class="submit small" name="submit" value="Submit" />
                                                    </p>
                                                </form> 

Thanks for your help.

EDIT

So anyone who runs into my problems, it's that $.get() is asynchronous, so it will always return false, or true depending on what submitForm is defined as. $.ajax() however, allows async to be set as false, which allows the function to finish before moving on. See what I mean:

        $(document).ready( function () {                                
            $("#add_account_form").submit( function () { 
                var submitForm = true;
                 var account_name = $("input[name=account_name]").val();
                    $.ajax({
                        type: "GET",
                        async: false,
                        url: "'.url::site("ajax/check_account_name").'", 
                        data: ({account_name: account_name}), 
                        success:
                    function(data){ 
                        if(data.length > 0){            
                                    if(!confirm( "The account name you entered looks like the following:\n"
                                                        +data+
                                                        "Press cancel if this account already exists or ok to create it."
                                                )){ 
                                                submitForm = false;
                                            }
                        }   
                    }
                    });

                        if (submitForm == false ) {
                    return false;     
                        }

                });
            });

Thanks for your help @Dan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山人契 2024-09-07 17:24:51

猜测一下,但也许 data.length 总是正确的?

我会尝试返回一个布尔值或只是一点点。

然后:

if(data) {
  confirm();
} else {
  $('#form-id').submit();
}

希望有帮助:)

编辑:误读问题

可能发生的情况是,当单击提交按钮时,表单在单击后提交。换句话说,您可能必须捕获 form.submit 事件:

$('form').submit(function() {
    var submitForm = true;
    var account_name = $("input[name=account_name]").val();
    $.get(
        "'.url::site("ajax/check_account_name").'", 
    {account_name: account_name}, 
    function(data){ 
        if(data.length > 0){
            if (!confirm("The account name you entered looks like the following:\n"
                        +data+
                        "Press cancel if this account already exists or ok to create it."
                        )) {
                submitForm = false;
        }                   
    });
    if (!submitForm) {
        return false;                
    }
});

Taking a bit of a guess, but perhaps data.length is always true?

I'd try returning a boolean or just a bit.

then:

if(data) {
  confirm();
} else {
  $('#form-id').submit();
}

Hope that helps :)

EDIT: Mis-read question

What may be happening is when the submit button is clicked, the form submits after the click. In other words, you may have to have a form.submit event caught:

$('form').submit(function() {
    var submitForm = true;
    var account_name = $("input[name=account_name]").val();
    $.get(
        "'.url::site("ajax/check_account_name").'", 
    {account_name: account_name}, 
    function(data){ 
        if(data.length > 0){
            if (!confirm("The account name you entered looks like the following:\n"
                        +data+
                        "Press cancel if this account already exists or ok to create it."
                        )) {
                submitForm = false;
        }                   
    });
    if (!submitForm) {
        return false;                
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文