dongye6377 2014-04-01 18:37
浏览 591
已采纳

jQuery验证submitHandler不显示响应

I have a jQuery validation script that is working perfectly with the except of the event handler. I have simplified this post for troubleshooting purposes.

jQuery

submitHandler: function (form) {
                $.ajax({
                    type: $(form).attr("method"),
                    url: $(form).attr("action"),
                    data: $(form).serialize(),
                    dataType : "json"
                })
                .done(function (data) {
                    if (data.resp > 0 ) {
                        alert(data.message);                       
                    }
                });
                return false; // required to block normal submit since you used ajax
            },
 // rest of the validation below, truncated for clarity

The submitHandler successfully posts to my PHP script that adds a user to a database and then echoes back a json_encode() result.

PHP

<?php
    // All processing code above this line has been truncated for brevity

    if($rows == "1"){
        $resp = array("resp"=>1, "message"=>"Account created successfully. Waiting for user activation.");
    }else{
        $resp = array("resp"=>2, "message"=>"User account already exists.");
    }

    echo json_encode($resp);
?>

As you can see the idea is simple. Alert the user with the proper response message. When I run my script the user account is added successfully to the database but no alert is displayed to the user. The Console in Chrome shows no errors, what am I missing?

  • 写回答

3条回答 默认 最新

  • dtgr6303 2014-04-02 18:19
    关注

    After some tinkering I was able to get things working the way I wanted. Here's the updated code.

    jQuery

    .done(function (data) {
    $("#user_add_dialog").dialog({
        autoOpen: false,
        modal: true,
        close: function (event, ui) {
        },
        title: "Add User",
        resizable: false,
        width: 500,
        height: "auto"
    });
    $("#user_add_dialog").html(data.message);
    $("#user_add_dialog").dialog("open");
    });
    
    return false; // required to block normal submit since you used ajax
    

    PHP

    <?php
        // All processing code above this line has been truncated for brevity
    
        if($rows == "1"){
            $resp = array("message"=>"Account created successfully. Waiting for user activation.");
        }else{
            $resp = array("message"=>"User account already exists.");
        }
    
        echo json_encode($resp);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!