jquery 检测文本框值的变化不是基于用户输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16048084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery detect textbox value change NOT based on user input
提问by Sammy
Let's say you have a simple Text Box and button on your page. Clicking on the button will update the textbox value:
假设您的页面上有一个简单的文本框和按钮。单击按钮将更新文本框值:
HTML:
HTML:
<input type="text" id="myText" val="" />
<input type="button" id="myBtn" value="Change TextBox Value" />
jQuery:
jQuery:
$("#myBtn").on('click', function() {
$("#myText").val('adsf');
})
I'd like to add an event listener for the change event of the textbox, like so:
我想为文本框的更改事件添加一个事件侦听器,如下所示:
$('#myText').on('change', function(){
alert('Changed!')
});
That alert does not get triggered when you click the button but it does get triggered when you manually change the textbox value and focus out of it.
当您单击按钮时不会触发该警报,但是当您手动更改文本框值并将焦点移出时它会被触发。
I know you can add $("#myText").trigger('change')
to the onclick event of the button, but I wish to only accomplish this by adding an event listener to the textbox.
我知道您可以添加$("#myText").trigger('change')
到按钮的 onclick 事件,但我希望只通过向文本框添加事件侦听器来完成此操作。
Thanks
谢谢
采纳答案by VisioN
There is no such event supported. The only thing you can try is to set interval or timeout to check if the value was changed (using datato store temporary value):
不支持此类事件。您唯一可以尝试的是设置时间间隔或超时以检查值是否已更改(使用数据存储临时值):
var $myText = $("#myText");
$myText.data("value", $myText.val());
setInterval(function() {
var data = $myText.data("value"),
val = $myText.val();
if (data !== val) {
$myText.data("value", val);
alert("changed");
}
}, 100);
回答by AxGryndr
Why not force a manual event when the button is clicked like so -
当按钮被点击时,为什么不强制手动事件 -
$('#myText').on('change',function(e){
alert('Changed!');
});
$("#myBtn").on('click', function() {
$("#myText").val('adsf');
$("#myText").change();
})
回答by Shah Nawaz
You need to simply use keyup
function:
您需要简单地使用keyup
功能:
$( "#myText").keyup(function(){
alert($(this).val());
});
回答by Stefan Avramovic
You can do something like this. Instead of listening for an input field listen to a change on div that gets updated att same time.
你可以做这样的事情。不是侦听输入字段,而是侦听 div 上同时更新的更改。
function trigger(){
setTimeout(function(){
document.getElementsByName("Thing")[0].value = 'hello'
document.getElementById('myDiv').innerHTML = document.getElementsByName("Thing")[0].value
}, 3000);
}
trigger()
document.getElementById("myDiv").addEventListener('DOMSubtreeModified', (e)=>{
console.log(e.target.innerHTML)
});
<input type="text" name="Thing" value="" />
<div style="display:none;" id="myDiv"></div>
回答by Kenneth
You'd need to connect both handlers. To reuse the handler you can extract it to a named function:
您需要连接两个处理程序。要重用处理程序,您可以将其提取到命名函数中:
function showChanged(){
alert('Changed!')
}
$("#myBtn").on('click', showChanged);
$("#myText").on('change', showChanged);