1

我创建了一个注册表单并使用 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>

提前致谢..

4

1 回答 1

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 回调中设置“return false”是没有意义的,它不会影响表单提交。

于 2012-12-18T08:30:02.177 回答