問題描述
這是我的 HTML 頁面
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"/>
當我按下按鈕時,文本框的值正在改變,并且 onchange 事件沒有觸發,但是當我在文本框中手動輸入值時,它會觸發 onchange.
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.
我想要的只是在單擊按鈕時在文本框中更改數據時調用 JavaScript 函數.
All I want is to call a JavaScript function when data is changed in the textbox when a button is clicked.
你能幫忙嗎?
推薦答案
您可以在通過更改值后使用 document.getElementById("txt").onchange();
從代碼中觸發 onchange代碼
you can fire onchange from code using document.getElementById("txt").onchange();
after the changing the value through code
更新
更改您的 set 函數以獲取文本框的 id
change your set function to take the id of the text box
function set(textboxid)
{
var tb = document.getElementById(textboxid);
tb.value++;
tb.onchange();
}
這篇關于將數據分配給輸入文本框時,輸入文本框 onchange 不會觸發的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!