$ form.submit(函数(){...});不管用

时间:2012-12-18 08:23:35

标签: php html jquery

我创建了一个Registrtion表单,我使用ajax来验证表单。我的问题是$ form.submit(function(){.....});没有用。我会解释这个问题。显示的是ajax的代码。

$(document).ready(function()
    {
        var $form=$('#myform');
        //debugger;
        $form.submit(function()
        {
            $.ajax({
                type:'POST',
                url:'registrationvalidation.php',
                data:$("#myform").serialize(),
                success:function(data)
                {
                    alert(data);
                    if(data=="ok")
                        {
                           return true;
                        }
                        else
                        {
                            alert(data);
                            return false;
                        }
                }
            });
        });
    });

并且提交被实时函数替换,它正在按要求工作。这是工作代码。

$(document).ready(function()
    {
        //var $form=$('#myform');

        //debugger;
        //$form.submit(function()
        $("#submit").live('click',function()
        {
            $.ajax({
                type:'POST',
                url:'registrationvalidation.php',
                data:$("#myform").serialize(),
                success:function(data)
                {
                    alert(data);
                    if(data=="ok")
                        {
                           return true;
                        }
                        else
                        {
                            alert(data);
                            return false;
                        }
                }
            });
        });
    });

但是在我的程序中我想使用提交功能,因为我在提交后将一些数据从html表单发布到另一个页面。当我使用chrome开发人员工具执行相同的代码(第一个代码)时,它正常工作。任何一个人知道解决方案.. Html表格

<form id="myform" action="insert.php" method="post" >
        <h1>Personal Information</h1>
        <fieldset id="personal">
            <input id="firstname" name="firstname" type="text" placeholder="FIRST NAME" autofocus><br>
            <input id="lastname" name="lastname" type="text" placeholder="LAST NAME"><br>
            <input id="gender" name="gender" type="text" placeholder="GENDER" ><br>
            <input id="email" name="email" type="email" placeholder="EMAIL"><br>
            <input id="date" name="date" type="date" placeholder="JOINING DATE" ><br>
            <input id="designation" name="designation" type="text" placeholder="DESIGNATION"><br>
        </fieldset>
        <h1>Login Info</h1>
        <fieldset id="logininfo">
            <input id="username" name="username" type="text" placeholder="USER NAME" ><br>
            <input id="password1" name="password1" type="password" placeholder="PASSWORD" ><br>
            <input id="password2" name="password2" type="password" placeholder="CONFORM PASSWORD" ><br>
        </fieldset>
        <h1>Permissions</h1>
        <fieldset id="permission" align="center">
            View&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:<input id = "VIEW" name="view" type = "checkbox" value = "view" /> <br>
            Edit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:<input id = "EDIT"name="edit" type = "checkbox" value = "edit" /> <br>
            Delete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:<input id = "DELETE"name="delete" type = "checkbox" value = "delete" /> <br>
            Permission&nbsp;&nbsp;:<input id = "PERMISSION" name="permission"type = "checkbox" value = "perms" /> <br>
            Add User&nbsp;&nbsp;&nbsp;:<input id = "ADD" name="add" type = "checkbox" value = "add" /> <br>
        </fieldset>
        <fieldset id="actions">
            <input type="button" id="submit" value="SUBMIT" />
            <!--<input type="submit" id="submit" value="SUBMIT" />this is for submit function-->
            <div id="idd"></div>
        </fieldset>
    </form>

提前致谢..

1 个答案:

答案 0 :(得分:1)

您应该取消表单提交并在AJAX成功时手动提交:

var oForm = $('#myform');
oForm.submit(function()
{
    $.ajax({
        type:'POST',
        url:'registrationvalidation.php',
        data: oForm.serialize(),
        success:function(data)
        {
            alert(data);
            if(data=="ok")
            {
                oForm[0].submit();
            }
            else
            {
                alert(data);
            }
        }
    });
    return false;
});

在AJAX回调中“返回false”是没有意义的,它不会影响表单提交。