jQuery not working on clicking radiobutton

六眼飞鱼酱① 提交于 2019-12-22 11:14:28

问题


my code:

$(document).ready(function(){
    alert("document ready");

    $("input[type=radio]").click(function(event){
        alert("clicked");
    });
});

But no alert shows up... And, when typing in on the console:
$(document).ready(function(){ alert("document ready");}) I get the alert! What is going on here?


回答1:


try .change() instead of .click()

$(document).ready(function(){
    alert("document ready");
    $("input[type=radio]").change(function(){
        alert("clicked");
    });
});



回答2:


I found the solution, it explains as well why it does work in JSFiddle.
The problem was how I imported jQuery: I did

<script type="text/javascript" src="jQuerylink" />

and it had to be

<script type="text/javascript" src="jQuerylink" ></script>

I don't know why this is so, but now it works. Thanks for all effort.




回答3:


$(function(){

    $("input[type='radio']").click(function(){
      alert("clicked");
    });        

});​

Example http://jsfiddle.net/Dw6Fp/2/




回答4:


According to the jQuery selector docs [link] you should enclose parameter value with double quotes:

$('input[type="radio"]').click(function(event){

OR

$("input[type=\"radio\"]").click(function(event){

UPDATE: JSFiddle example



来源:https://stackoverflow.com/questions/9751035/jquery-not-working-on-clicking-radiobutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!