Javascript event that fires without user interaction?

后端 未结 7 1345
离开以前
离开以前 2021-01-22 06:59

A textbox on my form may change depending on what\'s selected in various drop down lists.

Is there a way to call a javascript function when the textbox value changes? <

7条回答
  •  北海茫月
    2021-01-22 07:34

    In web browsers, programmatic changes on a control won't trigger events on that control. You must instruct the code that changes the textbox value to trigger the onchange event. This is how you can do it in plain javascript:

    var textbox = document.getElementById("boxId");
    textbox.value = "Abc";
    textbox.onchange();
    

    Of course, using a library like jQuery makes it easier and more robust:

    $("#boxId").val("Abc").change();
    

提交回复
热议问题