2017年12月1日金曜日

[jQuery] 追加した要素にイベントを設定/Setup Event to added element

<問題・Problem>

append()などを使用し、要素を追加すると、以下のように書いたイベントが動かきません。
When adding elements by using like "append()", the following code doesn't work.

  • $('.sample').change(function(){...
  • $('.sample').onclick(function(){...

<解決策・Solution>

以下のようにコードを書き換えます。
Replace the code with the following.
  • $('.sample').on('change',function(){...
  • $('.sample').on('click',function(){...

<注意・Note>

これでも動かない場合、以下のように$(function(){で囲み忘れている可能性があります。
If your code still doesn't work, be sure to use " $(function(){ " outside the code.

$(function(){
    $('.sample').on('click',function(){...
    });
});

0 件のコメント:

コメントを投稿