JQuery AJAX 提交按钮有时不起作用

发布于 2024-10-17 02:00:36 字数 2540 浏览 1 评论 0原文

大家好,

我使用链接作为 JQuery AJAX 提交按钮,有时单击时会触发,有时则不会。点火失败是间歇性的。看起来好像是在提交 AJAX 表单之后发生的,但返回时出现了我显示的错误。

然后,当我再次单击提交按钮/链接时,有时什么也没有发生,有时工作正常。控制台上没有显示 JavaScript 错误。这是我的代码的样子。我会很感激你的想法。我需要使用 .live() 吗?如果是这样,为什么?我想也许我不明白绑定是如何完全工作的。

谢谢, -Northk

// make the comment form submit link behave as if it's a submit button.
// this code is inside $(document).ready.  Corresponding HTML looks like:
//      <div class="box round-box darker comment-message-box">
//          <p class="comment-message"></p>
//      </div>    
//
// <a href="#" id="comment-button" class="action-button"><small>Submit</small></a>      
//
$(function() {
    $('#comment-button').click(function(e) {
        e.preventDefault(); // prevent the browser from "jumping" on the page when it comes back from AJAX. 
        $('.comment-message-box').hide(); // clear any leftover errors that may be showing on the form    
            $('#comment_form').submit(); 
    });
});

抱歉,这是 AJAX 代码。我试图让我的问题简短,但可能没有提供足够的代码上下文:

$(function() {
    $('#comment_form').ajaxForm({
        url: 'http://www.mywebsite.com',
        success: function(data) {
            if (data.match(/<title>Error<\/title>/)) {
                //grab the error message
                var error = $(data).find('ul li:first').text();

                // if they didn't enter any comment, this is the error we'll get back from the server
                if (error == 'The comment field is required')
                {
                    $('.comment-message').replaceWith('<p class="comment-message mtm mbm">Please enter a comment.</p>');
                } 
                // else some other kind of error occurred
                else  
                {
                    $('.comment-message').replaceWith('<p class="comment-message mtm mbm">I&rsquo;m sorry! for some reason the comment form isn&rsquo;t working at this time. Please <a href="/contact">contact me</a> for help.</p>');                      
                }
            }
            else {
                 // else no error, fix up a success message
                $('.comment-message').replaceWith('<p class="comment-message mtm mbm">Thanks for your comment!</p>');
                }
                // display the return message box
                $('.comment-message-box').fadeIn(1000); 
            },
        dataType: 'html'
    });
});

Greetings all,

I'm using a link as an JQuery AJAX submit button, and sometimes it fires when clicked and sometimes it doesn't. The failure to fire is intermittent. It seems like it happens after the AJAX form is submitted but returns with errors which I display.

Then when I click the submit button/link again sometimes nothing happens, and sometimes it works fine. No javascript errors show on the console. Here is what my code looks like. I would appreciate your thoughts. Do I need to be using .live()? And if so, why? I'm thinking maybe I don't understand how the binding works fully.

Thanks,
-Northk

// make the comment form submit link behave as if it's a submit button.
// this code is inside $(document).ready.  Corresponding HTML looks like:
//      <div class="box round-box darker comment-message-box">
//          <p class="comment-message"></p>
//      </div>    
//
// <a href="#" id="comment-button" class="action-button"><small>Submit</small></a>      
//
$(function() {
    $('#comment-button').click(function(e) {
        e.preventDefault(); // prevent the browser from "jumping" on the page when it comes back from AJAX. 
        $('.comment-message-box').hide(); // clear any leftover errors that may be showing on the form    
            $('#comment_form').submit(); 
    });
});

Sorry, here is the AJAX code. I was trying to keep my question short but probably didn't give enough code-context:

$(function() {
    $('#comment_form').ajaxForm({
        url: 'http://www.mywebsite.com',
        success: function(data) {
            if (data.match(/<title>Error<\/title>/)) {
                //grab the error message
                var error = $(data).find('ul li:first').text();

                // if they didn't enter any comment, this is the error we'll get back from the server
                if (error == 'The comment field is required')
                {
                    $('.comment-message').replaceWith('<p class="comment-message mtm mbm">Please enter a comment.</p>');
                } 
                // else some other kind of error occurred
                else  
                {
                    $('.comment-message').replaceWith('<p class="comment-message mtm mbm">I’m sorry! for some reason the comment form isn’t working at this time. Please <a href="/contact">contact me</a> for help.</p>');                      
                }
            }
            else {
                 // else no error, fix up a success message
                $('.comment-message').replaceWith('<p class="comment-message mtm mbm">Thanks for your comment!</p>');
                }
                // display the return message box
                $('.comment-message-box').fadeIn(1000); 
            },
        dataType: 'html'
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文