javascript - jQuery 动画可以工作,但并不总是有效

标签 javascript jquery

我有一个表单,其中包含带有 type="image" 属性的 input。每次无论我接到成功还是失败的电话,我都会更改图像。

但它并不总是有效,我不明白为什么。有没有可能的方法来消除 setTimeout(function()) 或实现此动画的替代方法,因为这可能会导致问题?

HTML

<p class="text-center" style="color:brown" id="input_result">Send</p>
<form data-ajax="false" method="post" name="login_form" role="form">
    <div class="form-group">
        <input type="image" src="images/default.png" class="img-responsive center-block" name="submit" class="submitBtn" id="input_img">
    </div>
</form>

脚本

<script>
    $('form').bind('submit', function() {


        $.ajax({
            type: 'POST',
            url: 'includes/sendmail.php',
            data: $(this).serialize(),
            cache: false,
            dataType: 'text',
            success: function(data, status, xhttp) {

                $("#input_img").slideUp(2000).slideDown(1000);
                setTimeout(function() {
                    // Success animation
                    $('#input_img').attr("src", "images/success.png");
                    jQuery("input[type='image']").prop("disabled", true);
                    $('selector').click(false);
                    $("#input_result").text("Sent!");
                    $("#input_result").css("color", "green");
                }, 1999);
                this.submit();
            },
            error: function(jqXHR, textStatus, errorThrown) {

                $("#input_img").slideUp(2000).slideDown(1000);
                setTimeout(function() {
                    // Fail animation
                    $('#input_img').attr("src", "images/fail.png");
                    jQuery("input[type='image']").prop("disabled", true);
                    $('selector').click(false);
                    $("#input_result").text("Failed to send!");
                    $("#input_result").css("color", "red");
                }, 1999);
            },
        });
        return false;
    })
</script>

最佳答案

当您使用 this.submit(); 提交表单时,页面将提交到服务器并再次重新加载,因此您会丢失成功动画。由于您在函数中发布值,因此无需提交表单。

关于javascript - jQuery 动画可以工作,但并不总是有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36447779/

相关文章:

javascript - 与 li 的 jQuery 图像交换

javascript - Vue.js文本输入不显示v-model的值

javascript - 如何使用 D3.js 正确缩放?

javascript - 访问请求 promise 中的 header 获取响应

javascript - FullCalendar 仅在周 View 和日 View 中显示事件,但不在月 View 中显示事件

javascript - 除了在输入字段中之外,是否基于 keyCode 运行 keyup function()?

javascript - 不使用 jquery 我想要一个弹出对话框来提供链接选择

javascript - jQuery 在 Firefox 中工作,但在 Chrome 中不工作

javascript - 如何用jquery改变背景图片

jquery - 为什么我的 $.ajax 请求总是失败?