开发者

How I can detect change on Select, which is called from code?

开发者 https://www.devze.com 2023-01-18 03:18 出处:网络
I want to trigger .c开发者_运维技巧hange() on select, if I change option in select element by code.

I want to trigger .c开发者_运维技巧hange() on select, if I change option in select element by code.

I take example from jQuery documentation and added my hook to link, which after click change selected option in select. And problem is if user click on that link, then change function doesn't react.

http://api.jquery.com/change/

$("select").change(function () {
      var str = "";
      $("select option:selected").each(function () {
            str += $(this).text() + " ";
          });
      $("div").text(str);
    })
    .change();

$("#test").click(function () {
  $("select option:eq(2)").attr("selected", "selected")
});   


the change function only reacts to user actions. You can call: $('select').change(); manually:

$("#test").click(function () {
  $("select option:eq(2)").attr("selected", true);
  $('select').change();
}); 

You should use an ID, instead of the jquery selecter: 'select'. Just in case you use multiple selects on 1 page.

0

精彩评论

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