ajax有时好用有时不好用,jquery - jQuery AJAX提交按钮有时不起作用 - 堆栈内存溢出...

大家问候

我使用链接作为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:

//

//

//

//

// Submit

//

$(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(/

Error/)) {

//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('

Please enter a comment.

');

}

// else some other kind of error occurred

else

{

$('.comment-message').replaceWith('

I’m sorry! for some reason the comment form isn’t working at this time. Please contact me for help.

');

}

}

else {

// else no error, fix up a success message

$('.comment-message').replaceWith('

Thanks for your comment!

');

}

// display the return message box

$('.comment-message-box').fadeIn(1000);

},

dataType: 'html'

});

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值