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

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

programmeradmin4浏览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/

发布评论

评论列表(0)

  1. 暂无评论