weixin_33724570 2016-02-05 10:17 采纳率: 0%
浏览 19

Ajax发送重复的头

Well I have stumbled upon a weird problem.

First time I send the form the server recieves 1. If I send another without reloading the page it sends 2 exact same posts, if I send it again it sends 3 and so on. If I would to send 9 forms it would send 9 exact same posts on the last.

Here's my code:

$('#AskACounsel .btn-ask-question').bind("click", function (e) {
    e.preventDefault();

    var textbox = $('#AskACounselQuestion');
    var question = textbox.val();    
    var product = textbox.data('productid');

    if (question.length > 0) {    
        var params = { question: question, productid: product };    
        var url = '/FAQService/AddQuestion';
        $.ajax({
            url: url, success: function (result) {    
                var infoDiv = $('#AskACounselThankYouView .counsel-info').html(result.Message);
                var backDiv = $('#AskACounselThankYouView .counsel-footer .btn-return');
                if (result.Success) {
                    textbox.val("");
                    backDiv.hide();
                } else {
                    backDiv.show();
                }

                $('#AskACounselDefaultView').hide();
                $('#AskACounselThankYouView').show();
            },
            type: 'POST',
            data: params    
        });
    }    
});
  • 写回答

1条回答 默认 最新

  • weixin_33691817 2016-02-05 10:23
    关注

    Its likely that the code you have is passed through many times, i.e. you have a function that has the code and calling it to handle the click event. The rule of thumb is that binding should happen ONLY ONCE, since it's a binding, not just an event handler like "on".

    See answer from https://stackoverflow.com/a/8065685/696034 If you want to continue using bind, then unbind before that code through $('#AskACounsel .btn-ask-question').unbind("click"); otherwise, bind the event in the initialization section of your code ONCE.

    评论

报告相同问题?