Trigger change event <select> using jquery

前端 未结 8 1175
别那么骄傲
别那么骄傲 2020-11-28 04:27

is there anyway i could trigger a change event on select box on page load and select a particular option.

Also the function executed by adding the trigger after bin

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 04:52

    Use val() to change to the value (not the text) and trigger() to manually fire the event.

    The change event handler must be declared before the trigger.

    Here's a sample

    $('.check').change(function(){
      var data= $(this).val();
      alert(data);            
    });
    
    $('.check')
        .val('two')
        .trigger('change');
    

提交回复
热议问题