簡體   English   中英

jQuery .submit()成功或失敗觸發器

[英]jQuery .submit() success or failure trigger

我正在使用.submit()定期在ajax上提交我的表單,但我希望用戶看到表單是用旋轉輪保存然后“保存!” 一旦成功。 jQuery中的.submit()是否有success觸發器?

謝謝!

嘗試這個:

jQuery的:

$(document).ready(function() {
    $('#form').submit(function() {
        var status = '<img class="loading" src="loading_detail.gif" alt="Loading..." />';
        $("#ajax").after(status);
        $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),
            dataType: 'json',
            success: function(json) {
                if(json.type == 'success') {
                    $('#msg').css("color","green").html(json.message);
                } else if(json.type == 'warning'){
                    $('#msg').css("color","yellow").html(json.message);
                } else if(json.type == 'error'){
                    $('#msg').css("color","red").html(json.message);
                }
                $('.loading').remove();
            }
        })
        return false;
    });
});

HTML:

<div id="msg"></div>
<form id="form" method="post" action="action.php" >
    <input type="text" name="email" />
    <input type="submit" name="submit" value="submit" /><span id="ajax"></span>
</form>

action.php:

<?php
if(isset($_POST['email'])){
$val = $_POST['email'];
switch ($val) {
    case 'email@site.com':
        $return = array('type'=>'success', 'message'=>'This is success message!'); break;
    case 'email':
        $return = array('type'=>'warning', 'message'=>'This is warning message!'); break;
    default:
        $return = array('type'=>'error', 'message'=>'This is error message!');
}
echo json_encode($return);
}
?>

注意:如果您以編程方式提交表單,則需要再次調用$('#form').submit() ,但這次不帶參數,以便觸發提交事件。

您可以使用手動$.post()請求而不是.submit() $.post()有一個成功的回調。

您必須填寫$.post() (目標URL)和.serialize()表單元素的詳細信息。

要在提交頁面時顯示旋轉輪,請嘗試獲取動畫GIF(有很多免費加載圈)。 這個GIF在您的頁面中實現:

class="myloadingcircle" style="display:none"

然后使用jQuery:

$("form").submit(function(e) { $(".myloadingcircle").show(); });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM