javascript - Do setting selected value of a select box trigger change event of the select list? - Stack Overflow

I am setting the selected value of a select list as below $("#typeFilter").val("0")

I am setting the selected value of a select list as below

$("#typeFilter").val("0");

My question is do making this, triggers $("#typeFilter").change event?

I am setting the selected value of a select list as below

$("#typeFilter").val("0");

My question is do making this, triggers $("#typeFilter").change event?

Share Improve this question edited May 16, 2013 at 6:20 shyam 9,3684 gold badges31 silver badges45 bronze badges asked May 16, 2013 at 6:18 Kuttan SujithKuttan Sujith 7,97918 gold badges66 silver badges96 bronze badges 2
  • 4 How about trying that out yourself and let us know too ? – gkalpak Commented May 16, 2013 at 6:21
  • Have you even tried to check if it does? – Mohayemin Commented May 16, 2013 at 6:23
Add a ment  | 

4 Answers 4

Reset to default 4

No it won't trigger the change event if you want to trigger the event after setting the value use trigger. Change event is triggered only if it is an interaction from device not with javascript.

$("#typeFilter").val("0");
$("#typeFilter").trigger('change');

Sample Demo

No, it doesn't! Unless the value is changed by user interaction, change event would not be triggered.
You can try it out yourself. Select any textbox's id. Lets say the id is "inputEmail". Try the following code:

$("#inputEmail").change(function() {
    alert("changed");
});

Then try the following line of code:

$("#inputEmail").val("abc");

Notice that the alert message did not appear.

No, it doesn't trigger it as it can bee seen in this live demo. You will need to trigger it manually after setting the value:

$("#typeFilter").change();

Nopes. It doesn't change. This is the code I used:

$(document).ready(function(){
    setTimeout(function(){
        $("#test").val("3");
    }, 1000);
    $("#test").change(function(){
        alert("Changed!");
    });
});

So, if you would like to trigger the change, you can use something like this:

$(document).ready(function(){
    $("#test").val("3").trigger("change");
    $("#test").change(function(){
        alert("Changed!");
    });
});

Fiddles:
http://jsfiddle/praveenscience/kLJuS/
http://jsfiddle/praveenscience/kLJuS/1/

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信