简体   繁体   English

jQuery Ajax点击仅可调用一次

[英]jQuery Ajax call on click only works once

I'm building a quiz for a clients website and I'm getting the questions from an Ajax call and then replacing the HTML content with the new question, the problem is my Ajax call only is working once and then fails if I try it again. 我正在为客户网站构建测验,并且从Ajax呼叫中获取问题,然后用新问题替换HTML内容,问题是我的Ajax呼叫仅工作一次,如果再次尝试将失败。 When I replace the content all at once with html() it fails, how ever if I break it down into sections and replace do html() for each section it works no problem but when I do it all at once if fails after the first time, if anyone could take a look at my code and tell me why this is happening it would be very much appreciated, thanks in advance! 当我一次用html()替换所有内容时,它会失败,但是如果我将其分解成几个部分并为每个部分替换html() ,那它不会有问题,但是当我在第一次替换后却全部失败时,我会立即全部替换它时间,如果有人可以看看我的代码并告诉我为什么会这样,将不胜感激,在此先感谢您!

HTML HTML

<section class="widget twelve quiz">
    <div class="content">
        <div class="widget six sign">
            <img src="http://www.mysite.com/images/questions/question-1.jpg" alt="">
        </div>
        <div class="widget six question">
            <header>
                <button class="align-right button" data-object='{"qui_id":"0","action":"1","que_id":"1"}'>Skip</button>
            </header>
            <h2>Q: Lorem Ipsum?</h2>
        </div>
        <div class="widget twelve answers">
            <ul>
                <li><button data-object='{"ans_id":"1"}'>A: Lorem 1.</button></li>
                <li><button data-object='{"ans_id":"2"}'>B: Lorem 2.</button></li>
                <li><button data-object='{"ans_id":"3"}'>C: Lorem 3</button></li>
                <li><button data-object='{"ans_id":"4"}'>D: Lorem 4.</button></li>
            </ul>
        </div>
        <div class="widget six navigation">
            <button class="align-right button" data-object='{"qui_id":"0","action":"0","que_id":"1"}'>Next</button>
        </div>
    </div>
</section>

Ajax Call only works once Ajax通话只能使用一次

$('.question,.navigation').on('click', '.button', function() {

    $.ajax({
        type: 'POST',
        url: 'http://www.mysite.com/handler-question.php',
        dataType: 'json',
        data: $(this).data('object'),
        success: function(data) {

            $('.quiz').html('<div class="content">'
                + '<div class="widget six sign">'
                    + '<img src="http://www.mysite.com/images/questions/'+data[0].que_file+'" alt="">'
                + '</div>'
                + '<div class="widget six question">'
                    + '<header>'
                          + '<button class="align-right button" data-object='+'{"qui_id":"'+data[0].qui_id+'","action":"1","que_id":"'+data[0].que_id+'"}'+'>Skip</button>'
                    + '</header>'
                    + '<h2>Q: '+data[0].que_question+'</h2>'
                + '</div>'
                + '<div class="widget twelve answers">'
                    + '<ul>'
                         + '<li><button data-object='+'{"ans_id":"'+data[1].ans_id+'"}'+'>A: '+data[1].ans_answer+'</button></li>'
                         + '<li><button data-object='+'{"ans_id":"'+data[2].ans_id+'"}'+'>B: '+data[2].ans_answer+'</button></li>'
                         + '<li><button data-object='+'{"ans_id":"'+data[3].ans_id+'"}'+'>C: '+data[3].ans_answer+'</button></li>'
                         + '<li><button data-object='+'{"ans_id":"'+data[4].ans_id+'"}'+'>D: '+data[4].ans_answer+'</button></li>'
                    + '</ul>'
                + '</div>'  
                + '<div class="widget six navigation">'
                    + '<button class="align-right button" data-object='+'{"qui_id":"'+data[0].qui_id+'","action":"0","que_id":"'+data[0].que_id+'"}'+'>Next</button>'
                + '</div>'
                + '</div>');

        }
    }); 
}); 

Ajax Call works no problem Ajax Call没问题

$('.question,.navigation').on('click', '.button', function() {

    $.ajax({
        type: 'POST',
        url: 'http://www.mysite.com/handler-question.php',
        dataType: 'json',
        data: $(this).data('object'),
        success: function(data) {

            $('.sign').html('<img src="http://www.mysite.com/images/questions/'+data[0].que_file+'" alt="">');

            $('.question').html('<header>'
                + '<button class="align-right button" data-object='+'{"qui_id":"'+data[0].qui_id+'","action":"1","que_id":"'+data[0].que_id+'"}'+'>Skip</button>'
                + '</header>'
                + '<h2>Q: '+data[0].que_question+'</h2>');

            $('.answers').html('<ul>'
                + '<li><button data-object='+'{"ans_id":"'+data[1].ans_id+'"}'+'>A: '+data[1].ans_answer+'</button></li>'
                + '<li><button data-object='+'{"ans_id":"'+data[2].ans_id+'"}'+'>B: '+data[2].ans_answer+'</button></li>'
                + '<li><button data-object='+'{"ans_id":"'+data[3].ans_id+'"}'+'>C: '+data[3].ans_answer+'</button></li>'
                + '<li><button data-object='+'{"ans_id":"'+data[4].ans_id+'"}'+'>D: '+data[4].ans_answer+'</button></li>'
                + '</ul>'); 

                $('.navigation').html('<button class="align-right button" data-object='+'{"qui_id":"'+data[0].qui_id+'","action":"0","que_id":"'+data[0].que_id+'"}'+'>Next</button>');

        }
    }); 
}); 

If you replace content with html() all event listeners attached to elements are lost, even if you are recreating the same markup again. 如果用html()替换内容,则即使重新创建相同的标记,所有附加到元素的事件侦听器也会丢失。 In fact the listeners are still in memory and clutter up the RAM consumption of the browser, but that's a different story. 实际上,侦听器仍在内存中,并且混乱了浏览器的RAM消耗,但这是另一回事。

You have two possibilities: 您有两种可能性:

  1. Recreate the event listeners once you replaced the html content. 替换html内容后,重新创建事件侦听器。 You should therefore assign the whole click listener callback to a variable. 因此,您应该将整个点击侦听器回调分配给一个变量。 But this is not the most maintainable and elegant solution. 但这不是最可维护且最优雅的解决方案。

  2. If possible, do not destroy elements that will still be used. 如有可能,请勿破坏仍将使用的元素。 Instead, just modify the elements that have changed (like in your working second example). 相反,只需修改已更改的元素(例如在您的第二个示例中)。 That way you can even use some highlighting animations to notify the user of the update. 这样,您甚至可以使用一些突出显示的动画来通知用户更新。

Also, i second what net.uk.sweet said in the comment section: generating html should be separated from your ajax javascript handling. 另外,我第二次在注释部分说了net.uk.sweet:生成html应该与您的ajax javascript处理分开。 Just update the necessary bits. 只需更新必要的位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM