241

に変更があったかどうかをキャプチャしたい<textarea>。任意の文字を入力する (削除、バックスペース) か、マウスのクリックと貼り付けまたは切り取りのように。これらすべてのイベントをトリガーできるjQueryイベントはありますか?

イベントを変更しようとしましたが、コンポーネントからタブアウトした後にのみコールバックがトリガーされます。

使用<textarea>:テキストが含まれている場合にボタンを有効にしたい。

4

11 に答える 11

356

実際にこれを試してください:

$('#textareaID').bind('input propertychange', function() {

      $("#yourBtnID").hide();

      if(this.value.length){
        $("#yourBtnID").show();
      }
});

デモ

これは、入力、切り取り、貼り付けなどの変更に対して機能します。

于 2012-07-05T06:09:46.840 に答える
151

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!");
});

jsFiddle デモ

于 2014-03-04T18:27:14.340 に答える
88

イベントを使用し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();
  }
});
于 2014-09-12T05:53:12.770 に答える
27

この質問には、ソースを含む最新の回答が必要でした。これは実際に機能するものです (ただし、私の言葉を鵜呑みにする必要はありません)。

// 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を確認することをお勧めします。

于 2015-12-24T06:42:56.027 に答える
4

これは別の (現代的な) ですが、前に述べたものとは少し異なるバージョンです。IE9 でテスト済み:

$('#textareaID').on('input change keyup', function () {
  if (this.value.length) {
    // textarea has content
  } else {
    // textarea is empty
  }
});

古いブラウザーの場合は、selectionchangeandを追加することもできますpropertychange(他の回答で述べたように)。しかしselectionchange、IE9ではうまくいきませんでした。それが私が追加した理由ですkeyup

于 2016-03-17T21:53:08.997 に答える
2

これを試して ...

$("#txtAreaID").bind("keyup", function(event, ui) {                          

              // Write your code here       
 });
于 2012-07-05T06:13:48.680 に答える
2

focusout でやってみる

$("textarea").focusout(function() {
   alert('textarea focusout');
});
于 2016-01-24T23:08:17.487 に答える
1

.delegateは、 jQuery JavaScript Library v2.1.1で動作している唯一のものです。

 $(document).delegate('#textareaID','change', function() {
          console.log("change!");
    });
于 2014-09-16T09:47:34.233 に答える
-11

これを試して

 $('textarea').trigger('change');
 $("textarea").bind('cut paste', function(e) { });
于 2012-07-05T06:12:19.087 に答える