4

This is my HTML page

<script>
function set()
{
document.getElementById("txt").value++;
}
</script>
<input id="txt" type="text" onchange="javascript:alert('txt changed');" value="0">
<br/>
<input type="button" onclick="set()" value="Set Data"/>

When I press the button, the text box value is changing, and the onchange event is not firing, but when I enter value manually in the textbox, it is firing the onchange.

All I want is to call a JavaScript function when data is changed in the textbox when a button is clicked.

Can you help?

4

2 回答 2

3

document.getElementById("txt").onchange();您可以在通过代码更改值之后从代码中触发 onchange

更新

更改您的 set 函数以获取文本框的 id

function set(textboxid)
{
    var tb = document.getElementById(textboxid);
    tb.value++;
    tb.onchange();
}
于 2010-08-30T13:21:00.920 回答
0

这似乎是一个浏览器错误。使用萤火虫,我已经在输入中记录了所有事件。当您使用函数 set() 更改值时会触发任何事件;

我在firefox中测试过。


注意:如果它不是错误,那么至少是关于事件如何工作的错误定义。

于 2010-08-30T13:45:39.210 回答