2017-09-11 102 views
0

我有从未执行的jQuery代码。当我在浏览器控制台中尝试使用相同的代码时,它的工作原理,所以我不明白为什么我的代码从未执行。jQuery代码从未执行

HTML代码:

<div id="ans"> 
    <label>{$valeur}</label> 
    <div class="radio"> 
     {foreach $reponse as $ligne} 
      <input type="radio" name="answers" id="answer_{[email protected]}" value="{[email protected]}"/><label for="answer_{[email protected]}">$ligne</label><br /> 
     {/foreach} 
    </div> 
</div> 

更新:

document.addEventListener("DOMContentLoaded", function() { 
    setTimeout(function(){ 
    $.post(ajax_path+"bla.php", {} , function(response) { 
      $('#quest').html(response); 
      $('#quest').collapse("show"); 
    }) 
    .fail(function(jqXHR, textStatus, errorThrown) { 
     {/literal} 
     toastr.error(__['Error'], __['Error']); 
     {literal} 
    }); 
    }, 50); 

    $('input[name=answers]').on('change', function() { 
     $(this).closest("form").submit(); 
    }); 
}); 
+3

你jQuery可能会在'DOMContentLoaded'之后加载。使用'$(document).ready' –

+1

为什么你以这种方式混合使用vanilla JS和jQuery? –

+0

已经尝试过,它不起作用。 – Mulan

回答

0
$(function() { 

    $('input[name=answers]').change(function() { 

     $(this).closest("form").submit(); 
    }); 

}); 

尝试用这种

此外,还要确保选择正确

编辑

当你想在所有的页面内容被加载,您可以使用负载,而不是准备

像这样

$(document).load(function() { 
    $('input[name=answers]').change(function() { 

     $(this).closest("form").submit(); 
    }); 
}) 

而且执行

,你可以使用窗口,而不是文件还

+0

它仍然无法正常工作。 选择器是正确的,因为当我在浏览器控制台中尝试此代码时,它的工作原理。 – Mulan

+0

我必须写('负载,功能(){...使其工作,但我的代码仍然不会执行:/ – Mulan