2

我正在使用 jQuery UI 对话框在我的网站上注册新用户。对话框工作正常。但是,当我提交表单时,AJAX 调用没有做任何事情。我已经在网上搜索了一段时间,但找不到有效的解决方案。这是我目前的代码:

var username=$("#username").val();
var password=$("#password").val();
var checkpassword=$("#checkpassword").val();
var email=$("#email").val();
var user_level=$("#user_level").val();
var date_created=$("#date_created").val();

$( "#dialog-form" ).dialog({
    autoOpen: false,
    height: 500,
    width: 600,
    modal: true,
    buttons: {
        "Registreren": function() {
            var bValid = true;
            allFields.removeClass( "ui-state-error" );

                bValid = bValid && checkLength( username, "username", 3, 16 );
                bValid = bValid && checkLength( email, "email", 6, 80 );
                bValid = bValid && checkLength( password, "password", 4, 16 );
                bValid = bValid && checkLength( checkpassword, "checkpassword", 4, 16 );
                bValid = bValid && checkLength( user_level, "user_level", 0, 2 );
                bValid = bValid && checkRegexp( username, /^[a-z]([0-9a-z_])+$/i, "error" );
                bValid = bValid && checkRegexp( email, "error");
                bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "error" );
                bValid = bValid && checkRegexp( checkpassword, /^([0-9a-zA-Z])+$/, "error" );

            if ( bValid ) {
                $.post({
                url: 'register.php',
                data: 
                        {  
                            username: username,
                            password: password,
                            checkpassword: checkpassword,
                            email: email,
                            user_level: user_level,
                            date_created: date_created
                        }//end data
                })//end AJAX post
           }//End if
        }//End function
    }//end buttons
});//end dialog

我的表单只是一个带有指定 ID 的简单 HTML 输入表单

例如输入用户名:

<form name="registeracc" id="registeracc" action="register.php" method="post">
<input required type="text" id="username" name="username" class="text ui-widget-content ui-corner-all" />
</form>     

我在网上找到了几个针对这个问题的“修复”,但它们似乎对我不起作用。

前任:

data: $('#registeracc :input').serialize(),
error: function(xml, status, error) {
$('#registeracc').html('<p>???</p>');
4

2 回答 2

2

我不确定,但根据jQuery API Reference尝试

$.post(
    'register.php',
    {  
        username: username,
        password: password,
        checkpassword: checkpassword,
        email: email,
        user_level: user_level,
        date_created: date_created
    }//end data
)//end AJAX post
于 2013-01-07T12:35:15.950 回答
2

找到了解决我问题的答案:

$.post({
url: 'register.php',
data: 
{  
   username: username,
   password: password,
   checkpassword: checkpassword,
   email: email,
   user_level: user_level,
   date_created: date_created
}

应该:

$.post("register.php", $("#registeracc").serialize());
于 2013-01-09T14:13:57.137 回答