javascript表单提交事件不起作用

时间:2014-07-11 07:29:02

标签: jquery post event-handling form-submit

我有用户注册页面,想要在注册开始之前将ajax发送到另一个页面。 我用'on'处理表单提交事件但是注册现在不起作用。提交表单后刷新我的页面。并且不向寄存器模块发送任何内容。

这是我的代码

function subdomainSent() {
    var subdomain = $('#subdomain').val();
    $.ajax({
        url: '/module/nmo/display',
        type: 'post',
        data: {
            action: 'subdomain_check',
            subdomain: subdomain
        },
        success: function (output) {
            alert(output);
            subdomain_complete();
        }
    });
}

function subdomain_complete() {
    document.main_form.submit();
}
$('#account-creation_form').on('submit', function (e) {
    e.preventDefault();
    subdomainSent();
});
if (getUrlParameter("action") == "create_account") {
    $("#account-creation_form").attr("action", "http://" + document.domain + window.location.pathname + "?action=create_account");
    $('#submitAccount').prop('disabled', true);
    $('#subdomain_show').css('display', 'block');
} else {
    $("#account-creation_form").attr("action", "http://" + document.domain + window.location.pathname);
}

并形成

<form name="main_form" action="" method="post" id="account-creation_form" class="std box">

更新 @BranimirĐurek使用的解决方案 像我这样改进了我的代码

if(start)
{
    var subdomain = $('#subdomain').val();
        $.ajax({
            url: '/module/nmo/display',
            type: 'post',
            data: {action: 'subdomain_check', subdomain: subdomain},
            success: function(output) {
                start = false;
            }
        });
}
else if(!start)
{
    subdomain_complete();
}

现在一切正常。

2 个答案:

答案 0 :(得分:1)

尝试替换

function subdomain_complete() {
    document.main_form.submit();
}

 function subdomain_complete() {
        $("#account-creation_form").submit();
    }

答案 1 :(得分:0)

首先,您在提交表单时尝试做两件不同的事情。 如果条件应该在$(document).ready()函数中, 并且所有功能都应该在doc准备好之外,

你的代码就像一个递归。所以只需改变它,如下所示,

 function subdomainSent() {
        var subdomain = $('#subdomain').val();
        $.ajax({
            url: '/module/nmo/display',
            type: 'post',
            data: {
                action: 'subdomain_check',
                subdomain: subdomain
            },
            success: function (output) {
                alert(output);
                subdomain_complete();
            }
        });
    }

    function subdomain_complete() {
        $("#account-creation_form).submit();
    }

    $(document).ready(function(){

$('#account-creation_form_save_btn').on('click', function (e) {
        e.preventDefault();
        subdomainSent();
    });


        if (getUrlParameter("action") == "create_account") {
            $("#account-creation_form").attr("action", "http://" + document.domain + window.location.pathname + "?action=create_account");
            $('#submitAccount').prop('disabled', true);
            $('#subdomain_show').css('display', 'block');
        } else {
            $("#account-creation_form").attr("action", "http://" + document.domain + window.location.pathname);
        }
});

<form id="account-creation_form">
<!-- your code here -->
<input type="button" id="account-creation_form_save_btn">
</form>

此处,account-creation_form_save_btn是一个按钮的id,您必须单击该按钮才能提交表单,并且不应提交按钮类型。

相关问题