I set field value w/ javascript, onchange not triggered. Alternative solution?

你。 提交于 2019-12-13 04:16:25

问题


I have read some solutions to this, none of which are hassle free, so I thought maybe some modern solution now exist to remedy this. It should anyways, since the problem has been around for a while. Something in the more recent jQuery updates perhaaps?

I set my form field like this:

parent.window.document.getElementById(parent.window.imageInputField).value = '{{ path }}'+image;

and I have an onchange event like this that I would like to fire upon my field update as of above.

$('#data_1').change(function(){
img = "url(" + $('#data_1').val() + ")"
$('#slide_bg').css("backgroundImage", img );
});

UPDATE

so this code works fine:

alert( $('#'+parent.window.imageInputField, window.parent.document).val() );

while this doesn't do anything, no error or nothing

$('#'+parent.window.imageInputField, window.parent.document).change();

The change event is defined here

$('#data_1').change(function(){
alert('change event called');
img = "url(" + $('#data_1').val() + ")"
$('#slide_bg').css("backgroundImage", img );
});

and this works perfect:

<input type='button' value='Uppdatera förhandsgr.' onclick="$('#data_1').change()" />

All I want is to trigger what this button does, but by code... anyone see where I went wrong?


回答1:


you need to trigger the change handler with your code. Updating a value with script will not trigger change event

parent.window.document.getElementById(parent.window.imageInputField).value = '{{ path }}'+image;
$('#data_1').change()


来源:https://stackoverflow.com/questions/14085251/i-set-field-value-w-javascript-onchange-not-triggered-alternative-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!