简体   繁体   English

如何使用Javascript来判断浏览器是否更改了HTML输入字段?

[英]How can I use Javascript to tell whether an HTML input field has been changed by the browser?

onchange="myfunction()"

The above works perfectly when I want the Javascript function "myfunction" to execute as soon as a user inputs some text into an input field however, I have a situation where the browser automatically inputs the text. 当用户在输入字段中输入一些文本时,我希望Javascript函数“myfunction”能够执行,但是,我有一种浏览器自动输入文本的情况。 How can I execute myfunction when the field is updated with the following: 如何使用以下内容更新字段时如何执行myfunction:

document.getElementById("myfield").value = "my value"

"onchange" does not recognise DOM changes. “onchange”无法识别DOM更改。

onchange only fires when the user types into the input and then the input loses focus. onchange仅在用户键入输入时触发,然后输入失去焦点。

But you can trigger the event using: 但您可以使用以下命令触发事件:

$("#myfield").trigger("change");

 $(function(){ $('#btn').click(function(){ document.getElementById('myField').value = "my value"; $('#myField').trigger('change'); }); }) function myfunction(){ alert('Value Changed'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type = "text" onChange = "myfunction()" id="myField"/> <button id="btn">Change </button> 

onchange only fires when the user types into the input and then the input loses focus. onchange仅在用户键入输入时触发,然后输入失去焦点。

But you can trigger the event using: 但您可以使用以下命令触发事件:

$("#myfield").trigger("change");

JSFIDDLE 的jsfiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断 HTML textarea 何时被 Javascript 更改 - How to tell when an HTML textarea has been changed by Javascript 如何判断JavaScript文件是否已包含在ASP.NET页面中? - How can I tell whether a JavaScript file has already been included in an ASP.NET page? 如何在输入字段中输入字符时如何判断? - How can I tell when a character has been typed in an input field? 我可以使用Javascript检测复选框是否被选中吗? - Can I use Javascript to detect whether a checkbox has been checked? 如何判断Backbone.js中的模型是否未更改? - How can I tell if a model has not been changed in Backbone.js? 如何确定用户或浏览器是否输入了表单上的数据? - How do I determine whether data on a form has been input by the user or the browser? 从浏览器拨打电话时,如何以编程方式告知呼叫何时被应答? - When making a call from the browser, how can I tell programmatically when the call has been answered? 如何得知浏览器故意取消了AJAX的时间? - How can I tell when AJAX has been intentionally canceled by the browser? 如何判断浏览器是否在具有JavaScript的触摸屏设备上? - How do I tell whether a browser is on a touchscreen device with JavaScript? 如果数据属性(HTML)=== javascript 中的输入文本,如何更改已写入输入的每个字母的 javascript 颜色? - How can I change color in javascript for each letter which has been written into an input if data attribute(HTML) === input text in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM