开发者

How can we run a function when I clicked on Radio button in jQuery

I have three radio buttons and when I click on one radio button I want to have some fun开发者_JAVA百科ctionality using jQuery.


This would fire the code when you click on (for example) the second radio. Is that what you meant?

Example: http://jsfiddle.net/z2f5j/

HTML

<input type='radio' name='somename' />one
<input type='radio' name='somename' />two
<input type='radio' name='somename' />​three​​​

jQuery

    // :eq() uses a 0 based index, so 1 is the 2nd radio
$(':radio[name=somename]:eq(1)').click(function() {
    alert('i was clicked');
});​

You could also use a change event, instead of click.

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

      // run code when the radio is selected
$(':radio[name=somename]:eq(1)').change(function() {
    alert('i was changed');
});​
  • http://api.jquery.com/eq/
  • http://api.jquery.com/click/
  • http://api.jquery.com/change/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜