问候所有人,
我正在使用链接作为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’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'
});
});