最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onChange event not firing when input changed dynamically - Stack Overflow

programmeradmin0浏览0评论

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 overe this?

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 overe this?

Share Improve this question edited Feb 19, 2014 at 17:24 J86 asked Feb 19, 2014 at 17:19 J86J86 15.3k50 gold badges145 silver badges249 bronze badges 3
  • Try blur event instead of change on input field. – Latheesan Commented Feb 19, 2014 at 17:21
  • you can use keyup,also your question is not clear – A.T. Commented Feb 19, 2014 at 17:21
  • I hope it is clearer now. I tried blue .. and got same thing, not firing. :( – J86 Commented Feb 19, 2014 at 17:24
Add a ment  | 

2 Answers 2

Reset to default 4

Since you're using jQuery, you can trigger the event:

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

you can simulate change event by

var element = document.getElementById('formElementFromEmail');
element.onchange();
发布评论

评论列表(0)

  1. 暂无评论