admin管理员组

文章数量:1405412

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/

本文标签: javascriptDo setting selected value of a select box trigger change event of the select listStack Overflow