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('
');}
// else some other kind of error occurred
else
{
$('.comment-message').replaceWith('
');}
}
else {
// else no error, fix up a success message
$('.comment-message').replaceWith('
');}
// display the return message box
$('.comment-message-box').fadeIn(1000);
},
dataType: 'html'
});
});
//
//