我设置了带有javascript的字段值,未触发onchange.替代解决方案? [英] I set field value w/ javascript, onchange not triggered. Alternative solution?
问题描述
我已经阅读了一些解决方案,但都不是轻松的,所以我认为现在可能存在一些现代的解决方案来解决这个问题.无论如何,应该都这样做,因为问题已经存在了一段时间.在最近的jQuery更新中有一些功能吗?
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;
并且我有一个像这样的onchange事件,我想在上面的字段更新时触发.
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();
更改事件在此处定义
$('#data_1').change(function(){
alert('change event called');
img = "url(" + $('#data_1').val() + ")"
$('#slide_bg').css("backgroundImage", img );
});
这很完美:
<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?
推荐答案
,您需要使用代码来触发变更处理程序.使用脚本更新值不会触发change
事件
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()
这篇关于我设置了带有javascript的字段值,未触发onchange.替代解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!