自动更改时onChange事件不起作用 [英] onChange event not working when change automated

查看:727
本文介绍了自动更改时onChange事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个选择框(usageDisplays)上有一个onChange事件,它根据第一个选定的值填充下一个选择框:

 < html:select name =usageDisplaysproperty =nameindexed =trueonchange ='<%=populateYearsList(+ Id +,+ name +)%> ;'/> 

当用户选择usageDisplays中的值时,此工作正常,但当usageDisplays的值根据页面前面的单选按钮设置

  getElementByName(usageDisplay.name)。value =用户名; 


解决方案

hi onchange只会在选择值必须改变时触发但在页面加载时,它不会首先选择任何内容,然后更改

以编程方式更改该值不会引发更改事件,仅当用户关注元素时才会触发更改事件,选项是手动调用onchange侦听器,在元素上发送一个change事件,或者通过向上调整change事件来手动冒泡。 DOM寻找带有onchange监听器的父母并给他们打电话。



下面是一个似乎符合法案的答案:手动触发onchange事件



一些链接:

  MDN dispatchEvent(符合标准):https://developer.mozilla。 org / en / DOM / element.dispatchEvent 
MSDN fireEvent(IE专有):http://msdn.microsoft.com/en-us/library/ie/ms536423(v=vs.85).aspx


I have an onChange event on one select box (usageDisplays), which populates the next select box based on the selected value of the first:

<html:select name="usageDisplays" property="name" indexed="true" onchange='<%="populateYearsList(" + Id + "," + name + ")"%>' />

This works fine when the user selects the value in usageDisplays, but doesn't work at all when the value of usageDisplays is set based on a radio button choice earlier on the page

getElementByName("usageDisplay.name").value = userName;

解决方案

hi onchange will fire only when the select value must change but on page load it will not select anything first and then change

Programmatically changing the value doesn't fire a change event, that only occurs if the user focuses the element, changes the value and then puts focus elsewhere.

Options are to manually call the onchange listener, dispatch a change event on the element, or manually bubble the change event by going up the DOM looking for parents with an onchange listener and calling them.

Here is an answer that seems to fit the bill: trigger onchange event manually

Some links:

MDN dispatchEvent (standards compliant): https://developer.mozilla.org/en/DOM/element.dispatchEvent
MSDN fireEvent (IE proprietary): http://msdn.microsoft.com/en-us/library/ie/ms536423(v=vs.85).aspx

这篇关于自动更改时onChange事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆