Javascript 如何绑定到 jQuery 中 textarea 的更改事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11338592/
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
How can I bind to the change event of a textarea in jQuery?
提问by Lolly
I want to capture if any changes happened to <textarea>
. Like typing any characters (deleting,backspace) or mouse click and paste or cut. Is there a jQuery event that can trigger for all those events?
我想捕获<textarea>
. 就像键入任何字符(删除、退格)或鼠标单击并粘贴或剪切一样。是否有可以触发所有这些事件的 jQuery 事件?
I tried change event, but it triggers the callback only after tabbing out from the component.
我尝试了更改事件,但它仅在从组件中退出后才触发回调。
Use: I want to enable a button if a <textarea>
contains any text.
使用:如果 a<textarea>
包含任何文本,我想启用一个按钮。
回答by Blaster
回答by SNag
bind
is deprecated. Use on
:
bind
已弃用。使用on
:
$("#textarea").on('change keyup paste', function() {
// your code here
});
Note: The code above will fire multiple times, once for each matching trigger-type. To handle that, do something like this:
注意:上面的代码将触发多次,每个匹配的触发器类型触发一次。要解决这个问题,请执行以下操作:
var oldVal = "";
$("#textarea").on("change keyup paste", function() {
var currentVal = $(this).val();
if(currentVal == oldVal) {
return; //check to prevent multiple simultaneous triggers
}
oldVal = currentVal;
//action to be performed on textarea changed
alert("changed!");
});
回答by Steel Brain
回答by Chris Fritz
This question needed a more up-to-date answer, with sources. This is what actuallyworks (though you don't have to take my word for it):
这个问题需要一个最新的答案,有来源。这才是真正有效的(虽然你不必相信我的话):
// Storing this jQuery object outside of the event callback
// prevents jQuery from having to search the DOM for it again
// every time an event is fired.
var $myButton = $("#buttonID")
// input :: for all modern browsers [1]
// selectionchange :: for IE9 [2]
// propertychange :: for <IE9 [3]
$('#textareaID').on('input selectionchange propertychange', function() {
// This is the correct way to enable/disabled a button in jQuery [4]
$myButton.prop('disabled', this.value.length === 0)
}
1: https://developer.mozilla.org/en-US/docs/Web/Events/input#Browser_compatibility
2: oninput in IE9 doesn't fire when we hit BACKSPACE / DEL / do CUT
3: https://msdn.microsoft.com/en-us/library/ms536956(v=vs.85).aspx
4: http://api.jquery.com/prop/#prop-propertyName-function
1:https: //developer.mozilla.org/en-US/docs/Web/Events/input#Browser_compatibility
2:当我们点击 BACKSPACE / DEL / do CUT
3时,IE9 中的 oninput 不会触发:https://msdn .microsoft.com/en-us/library/ms536956(v=vs.85).aspx
4:http: //api.jquery.com/prop/#prop-propertyName-function
BUT, for a more global solution that you can use throughout your project, I recommend using the textchange jQuery pluginto gain a new, cross-browser compatible textchange
event. It was developed by the same personwho implemented the equivalent onChange
event for Facebook's ReactJS, which they use for nearly their entire website. And I think it's safe to say, if it's a robust enough solution for Facebook, it's probably robust enough for you. :-)
但是,对于可以在整个项目中使用的更全局的解决方案,我建议使用textchange jQuery 插件来获得一个新的、跨浏览器兼容的textchange
事件。它是由为 Facebook 的 ReactJS实现等效事件的同一个人开发onChange
的,他们几乎将其用于整个网站。而且我认为可以肯定地说,如果它对 Facebook 来说是一个足够强大的解决方案,那么它对你来说可能也足够强大。:-)
UPDATE:If you happen to need features like drag and drop support in Internet Explorer, you may instead want to check out pandell
's more recently updated fork of jquery-splendid-textchange
.
更新:如果您碰巧需要 Internet Explorer 中的拖放支持等功能,您可能需要查看pandell
最近更新的jquery-splendid-textchange
.
回答by rap-2-h
2018, without JQUERY
2018 年,没有 JQUERY
The question is withJQuery, it's just FYI.
现在的问题是与JQuery的,它只是供参考。
JS
JS
let textareaID = document.getElementById('textareaID');
let yourBtnID = document.getElementById('yourBtnID');
textareaID.addEventListener('input', function() {
yourBtnID.style.display = 'none';
if (textareaID.value.length) {
yourBtnID.style.display = 'inline-block';
}
});
HTML
HTML
<textarea id="textareaID"></textarea>
<button id="yourBtnID" style="display: none;">click me</div>
回答by kevinweber
Here's another (modern) but slightly different version than the ones mentioned before. Tested with IE9:
这是另一个(现代)但与前面提到的版本略有不同的版本。用IE9测试:
$('#textareaID').on('input change keyup', function () {
if (this.value.length) {
// textarea has content
} else {
// textarea is empty
}
});
For outdated browsers you might also add selectionchange
and propertychange
(as mentioned in other answers). But selectionchange
didn't work for me in IE9. That's why I added keyup
.
对于过时的浏览器,您还可以添加selectionchange
和propertychange
(如其他答案中所述)。但selectionchange
在 IE9 中对我不起作用。这就是为什么我添加了keyup
.
回答by llioor
Try to do it with focusout
尝试通过 focusout 做到这一点
$("textarea").focusout(function() {
alert('textarea focusout');
});
回答by eeadev
.delegateis the only one that is working to me with jQuery JavaScript Library v2.1.1
.delegate是唯一一个使用jQuery JavaScript Library v2.1.1对我有用的
$(document).delegate('#textareaID','change', function() {
console.log("change!");
});
回答by stay_hungry
try this ...
尝试这个 ...
$("#txtAreaID").bind("keyup", function(event, ui) {
// Write your code here
});
回答by Rooster242
After some experimentation I came up with this implementation:
经过一些实验,我想出了这个实现:
$('.detect-change')
.on('change cut paste', function(e) {
console.log("Change detected.");
contentModified = true;
})
.keypress(function(e) {
if (e.which !== 0 && e.altKey == false && e.ctrlKey == false && e.metaKey == false) {
console.log("Change detected.");
contentModified = true;
}
});
Handles changes to any kind of input and select as well as textareas ignoring arrow keys and things like ctrl, cmd, function keys, etc.
处理对任何类型的输入和选择以及文本区域的更改,忽略箭头键和 ctrl、cmd、功能键等。
Note: I've only tried this in FF since it's for a FF add-on.
注意:我只在 FF 中尝试过这个,因为它用于 FF 附加组件。