简体   繁体   中英

onChange event not firing when input changed dynamically

On my form, I have a DropDown, when a value is selected, I fire some JavaScript that populates some visible <input type="text" /> fields (under neath the drop down).

The problem is, I also want to run some JavaScript function whenever the value of one of these inputs changes, I want this function to run even if the text input is changed by the drop down selection.

Here is my current code:

jQuery(document).ready(function() {
    $('#mainPane').on('change', '#formElementFromEmail', function(event){
        var collectFromDropDown = $('#formElementFromEmail');
        persistHiddenInputForCollectFromValue();
    });
});

I suspect the change event is not being fired because no one has manually changed the value of the field in question. How can I overcome this?

由于您使用的是jQuery,因此可以触发事件:

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

you can simulate change event by

var element = document.getElementById('formElementFromEmail');
element.onchange();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM