最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - call change() event handler if I select radio button programatically - Stack Overflow

programmeradmin4浏览0评论

I have HTML like this:

<input type="radio" name="type" value="FL" checked="checked" />Fixed
<input type="radio" name="type" value="SV" />Saving
<input type="radio" name="type" value="L2" />Type 2

And following script

$(function () {
        $('input[name=type]').change(function () {
            alert($(this).val());
        });
        $('input[value=SV]').attr('checked', 'checked');
    });

Firstly, have added a change event to radio button. change event handler triggered if I am selecting radio button from UI. But it does not triggered when I change selected radio button value pro-grammatically.

I want change event also to be triggered when I select radio button pro-grammatically.

I have HTML like this:

<input type="radio" name="type" value="FL" checked="checked" />Fixed
<input type="radio" name="type" value="SV" />Saving
<input type="radio" name="type" value="L2" />Type 2

And following script

$(function () {
        $('input[name=type]').change(function () {
            alert($(this).val());
        });
        $('input[value=SV]').attr('checked', 'checked');
    });

Firstly, have added a change event to radio button. change event handler triggered if I am selecting radio button from UI. But it does not triggered when I change selected radio button value pro-grammatically.

I want change event also to be triggered when I select radio button pro-grammatically.

Share Improve this question edited Oct 9, 2015 at 2:45 shashwat asked Apr 1, 2012 at 14:53 shashwatshashwat 8,0049 gold badges60 silver badges94 bronze badges 5
  • You choose a bad nickname. what will you do next year...? – gdoron Commented Apr 1, 2012 at 15:14
  • I will change it to my real name after a few days only. – shashwat Commented Apr 1, 2012 at 15:17
  • possible duplicate of Checkbox change event not firing ... please use the search before you ask a new question. – Felix Kling Commented Apr 1, 2012 at 15:58
  • @new to stackoverflow, adding a ment to every response asking for upvote isn't a good policy and won't get you more upvotes; asking a good question will. And your ments may get flagged. – Mahm00d Commented Apr 22, 2012 at 13:51
  • @FlomEnol Dear, sorry for that..bt that tym I could not even vote up answers. Thats because I did it. Now I hv removed any such cmnts. Sorry again – shashwat Commented Apr 23, 2012 at 6:06
Add a ment  | 

2 Answers 2

Reset to default 11

You can use trigger() to programmatically raise an event:

$(function () {
  $('input[name="type"]').change(function() {
    console.log($(this).val());
  });
  $('input[value="SV"]').prop('checked', true).trigger('change');        
});

It won't change automatically. You have to do one of the following:

either

$('input[value=SV]').attr('checked', 'checked').trigger("change")

or

$('input[value=SV]').attr('checked', 'checked').change();
发布评论

评论列表(0)

  1. 暂无评论