select 选项改变事件问题

jyqxj 2011-11-15 09:40:30

<script language="javascript" type="text/javascript">
function change(){
alert("dd");
}
function clk(){
var selOption=document.getElementById("select");
selOption.selectedIndex=2;
}
</script>


<body>
<input type="button" onclick="clk()" value="click" />
<select name="select" id="select" onchange="change()">
<option>aa</option>
<option>bb</option>
<option>cc</option>
</select>
</body>


问题:在页面选择能激发改变事件?通过按钮改变被选中项就不能激发事件

如何在js代码改变被选中项激发,将激发什么事件呢





...全文
646 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
曹怡帅 2011-11-16
  • 打赏
  • 举报
回复
你可以查看下手册说明,对onchange事件有明确的解释,事件被commit而不是value变化的时候触发,

This event is fired when the contents are committed and not while the value is changing. For example, on a text box, this event is not fired while the user is typing, but rather when the user commits the change by leaving the text box that has focus. In addition, this event is executed before the code specified by onblur when the control is also losing the focus.

The onchange event does not fire when the selected option of the select object is changed programatically.
你可以看大概的表述,就是当选中项不是通过鼠标或者键盘点击改变的时候是不会触发的。
hch126163 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shen823797837 的回复:]

在程序中改变选择项是不会触发select的onChange()事件的,必须显示地触发此时间,也就是在
JScript code
selOption.selectedIndex=2;

后面加上一行
JScript code
selOption.onchange();

来显示触发,这样不管你在onChange()事件中指定任何方法,它都会自动调用了
[/Quote]

++
Legend1988 2011-11-16
  • 打赏
  • 举报
回复
+1.onchange和onpropertychange的不同之处在于onpropertychange是实时触发的,而onchange必须在当前元素失去焦点时才会触发。[Quote=引用 4 楼 hookee 的回复:]

HTML code

<script type="text/javascript">
function change(){
alert("dd");
}
function clk(){
var selOption=document.getElementById("select");
selOption.selectedIndex=2;
}

</script>
<inp……
[/Quote]
闪电侠的博客 2011-11-15
  • 打赏
  • 举报
回复
你可以看下这段代码

<input id="btn" type="button" onclick="clk()" value="click" />
<script language="javascript" type="text/javascript">
function clk(){
alert('按钮被点击!');
document.getElementById('btn').click();//这里的click()方法并不会触发onClick()事件(就像selOption.selectedIndex=2并不会触发onChange()事件一样),这种机制其实就是为了避免循环嵌套
}
</script>
hookee 2011-11-15
  • 打赏
  • 举报
回复

<script type="text/javascript">
function change(){
alert("dd");
}
function clk(){
var selOption=document.getElementById("select");
selOption.selectedIndex=2;
}

</script>
<input type="button" onclick="clk()" value="click" />
<select name="select" id="select" onpropertychange="change()">
<option>aa</option>
<option>bb</option>
<option>cc</option>
</select>
闪电侠的博客 2011-11-15
  • 打赏
  • 举报
回复
在程序中改变选择项是不会触发select的onChange()事件的,必须显示地触发此时间,也就是在
selOption.selectedIndex=2;

后面加上一行
selOption.onchange();

来显示触发,这样不管你在onChange()事件中指定任何方法,它都会自动调用了
闪电侠的博客 2011-11-15
  • 打赏
  • 举报
回复
在程序中改变选择项是不会触发select的onChange()事件的,必须显示地触发此时间,也就是在
selOption.selectedIndex=2;

后面加上一行
[code=JScriptselOption.onchange()];[/code
来显示触发,这样不管你在onChange()事件中指定任何方法,它都会自动调用了
p2227 2011-11-15
  • 打赏
  • 举报
回复
我的个人看法是不会触发什么事件,这样就好

<script language="javascript" type="text/javascript">
function change(){
alert("dd");
}
function clk(){
var selOption=document.getElementById("select");
selOption.selectedIndex=2;
change();
}
</script>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧