に変更があったかどうかをキャプチャしたい<textarea>
。任意の文字を入力する (削除、バックスペース) か、マウスのクリックと貼り付けまたは切り取りのように。これらすべてのイベントをトリガーできるjQueryイベントはありますか?
イベントを変更しようとしましたが、コンポーネントからタブアウトした後にのみコールバックがトリガーされます。
使用<textarea>
:テキストが含まれている場合にボタンを有効にしたい。
に変更があったかどうかをキャプチャしたい<textarea>
。任意の文字を入力する (削除、バックスペース) か、マウスのクリックと貼り付けまたは切り取りのように。これらすべてのイベントをトリガーできるjQueryイベントはありますか?
イベントを変更しようとしましたが、コンポーネントからタブアウトした後にのみコールバックがトリガーされます。
使用<textarea>
:テキストが含まれている場合にボタンを有効にしたい。
実際にこれを試してください:
$('#textareaID').bind('input propertychange', function() {
$("#yourBtnID").hide();
if(this.value.length){
$("#yourBtnID").show();
}
});
これは、入力、切り取り、貼り付けなどの変更に対して機能します。
bind
廃止されました。使用on
:
$("#textarea").on('change keyup paste', function() {
// your code here
});
注: 上記のコードは、一致するトリガー タイプごとに 1 回、複数回起動します。それを処理するには、次のようにします。
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!");
});
イベントを使用しinput
ます。
var button = $("#buttonId");
$("#textareaID").on('input',function(e){
if(e.target.value === ''){
// Textarea has no value
button.hide();
} else {
// Textarea has a value
button.show();
}
});
この質問には、ソースを含む最新の回答が必要でした。これは実際に機能するものです (ただし、私の言葉を鵜呑みにする必要はありません)。
// 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: IE9 の oninput は、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
しかし、プロジェクト全体で使用できるよりグローバルなソリューションについては、textchange jQuery プラグインを使用して、クロスブラウザー互換の新しいイベントを取得することをお勧めしますtextchange
。これは、Facebook の ReactJSの同等のイベントを実装した同じ人物によって開発されたもので、Web サイトのほぼ全体で使用されています。onChange
そして、それが Facebook にとって十分に堅牢なソリューションであるなら、おそらくあなたにとっても十分に堅牢であると言っても過言ではありません。:-)
更新: Internet Explorer でのドラッグ アンド ドロップ サポートなどの機能が必要な場合は、代わりに、pandell
最近更新された のフォークjquery-splendid-textchange
を確認することをお勧めします。
これは別の (現代的な) ですが、前に述べたものとは少し異なるバージョンです。IE9 でテスト済み:
$('#textareaID').on('input change keyup', function () {
if (this.value.length) {
// textarea has content
} else {
// textarea is empty
}
});
古いブラウザーの場合は、selectionchange
andを追加することもできますpropertychange
(他の回答で述べたように)。しかしselectionchange
、IE9ではうまくいきませんでした。それが私が追加した理由ですkeyup
。
これを試して ...
$("#txtAreaID").bind("keyup", function(event, ui) {
// Write your code here
});
focusout でやってみる
$("textarea").focusout(function() {
alert('textarea focusout');
});
.delegateは、 jQuery JavaScript Library v2.1.1で動作している唯一のものです。
$(document).delegate('#textareaID','change', function() {
console.log("change!");
});
これを試して
$('textarea').trigger('change');
$("textarea").bind('cut paste', function(e) { });