weixin_33696106 2016-05-01 21:29 采纳率: 0%
浏览 25

jQuery ajax只工作一次

I'm creatin a quiz bundle and i'm using ajax to load a new question form when submitting one . the problem is that the ajax is working only once when i click next i have a new form but then i keep having the old form .

Any Help ?

this is my view code :

<form method="post" class="ajax"> 
    <div id="form_body">

        {% include 'MoocBundle:Quiz:form.html.twig' with {'form': form} %}
    </div>   

</form>
<script type="text/javascript" src="{{ asset('bundles/mooc/js/jquery-2.1.4.min.js') }}"></script>
<script>
$(document).ready(function(){
        $( ".next" ).on("click", function(e) {

         e.preventDefault();

            $.ajax({
                type: $(this).attr('method'),
                url: "{{ path('quiz_show_next',{'id' : 1}) }}",
                cache: false,

                success: function (data) {
                     $('#form_body').html(data);
                  }
            });
            return false;
        }
        ); 
    });


</script>

my controller action ( i'm creating a new form each time )

public function nextAction(Request $request , $id )
{
        $em = $this->getDoctrine()->getManager();
        $quiz = $em->getRepository('MoocBundle:Quizz')->findOneById($id);
        $this->i++;

        $question = $quiz->getQuestions()->get($this->i);


        $form = $this->createCreateForm($question);
        return $this->render('MoocBundle:Quiz:takeQuiz.html.twig', array('form' => $form->createView()));

}
  • 写回答

2条回答 默认 最新

  • weixin_33736048 2016-05-01 21:45
    关注

    If .next is inside of #form_body then use event delegation. Change:

    $( ".next" ).on("click", function(e) {
    

    To:

    $('#form_body').on('click', '.next', function(e) {
    

    Each time you load ajax content you're replacing .next. Since the .next loaded by ajax was not present at DOM ready, the same code that fired for the original .next would not fire, unless you use event delegation.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配