javascript - Detect checkbox has been click with JQuery? - Stack Overflow

What wrong with the my code. When I click on the checkbox, nothing happen$(document).ready(function(){$

What wrong with the my code. When I click on the checkbox, nothing happen

$(document).ready(function(){
     $('input:checkbox[name=drawingNo]').click(function(){alert('I am here');});            
});  
...
<body>
    <form>
        <input type="checkbox" name="drawingNo" value="1"> 1 <br>
        <input type="checkbox" name="drawingNo" value="2"> 2 <br>
        <input type="checkbox" name="drawingNo" value="3"> 3 <br>
        <input type="checkbox" name="drawingNo" value="4"> 4 <br>
    </form>
</body>

EDIT: The above code worked fine. What happen to me is that, the tag <input type="checkbox"> are generated by some other script, so when document.ready() fire up, it cant register click event to checkbox, since these checkbox are not really there yet. So to fixed it: change .click() into .live('click', function(){...})

What wrong with the my code. When I click on the checkbox, nothing happen

$(document).ready(function(){
     $('input:checkbox[name=drawingNo]').click(function(){alert('I am here');});            
});  
...
<body>
    <form>
        <input type="checkbox" name="drawingNo" value="1"> 1 <br>
        <input type="checkbox" name="drawingNo" value="2"> 2 <br>
        <input type="checkbox" name="drawingNo" value="3"> 3 <br>
        <input type="checkbox" name="drawingNo" value="4"> 4 <br>
    </form>
</body>

EDIT: The above code worked fine. What happen to me is that, the tag <input type="checkbox"> are generated by some other script, so when document.ready() fire up, it cant register click event to checkbox, since these checkbox are not really there yet. So to fixed it: change .click() into .live('click', function(){...})

Share Improve this question edited Mar 10, 2010 at 23:56 Thang Pham asked Mar 10, 2010 at 23:15 Thang PhamThang Pham 38.7k79 gold badges208 silver badges289 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Your selector is wrong..

The below is the best format

$(document).ready(function(){
 $("input[name='drawingNo']").live('click', function(){
 alert('I am here');
 });            
});

It works fine for me in IE, FF and Chrome.

Use $.fn.live ...

$('input:checkbox[name=drawingNo]').live('click',function(){
   alert('I am here');
});

try removing ':checkbox' so that it looks like:

$('input[name=drawingNo]').click(function(){alert('I am here');});

any better?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744759211a4592069.html

相关推荐

  • javascript - Detect checkbox has been click with JQuery? - Stack Overflow

    What wrong with the my code. When I click on the checkbox, nothing happen$(document).ready(function(){$

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信