jquery invoke .change without user action but by .val change

后端 未结 3 867
盖世英雄少女心
盖世英雄少女心 2021-01-23 14:25

I have a form with a text box that has its value changed from a function. How would I create a function based on the value changing from the script and not the user \"clicking\"

相关标签:
3条回答
  • 2021-01-23 14:53

    You will need to call the change yourself in the code which updates the value:

    $('#ShippingCost').val("something")
                      .change();  // this will execute the change code
    

    Changing values through javascript does not fire events.

    0 讨论(0)
  • 2021-01-23 14:59

    I'd just blur the input, so the change triggers on its own.

    $('#ShippingCost').val('changed value').blur();
    
    0 讨论(0)
  • 2021-01-23 15:08

    You need to trigger the event manually:

    $('#ShippingCost').trigger('change');

    0 讨论(0)
提交回复
热议问题