2016-03-20 4 views
0

は、私は私のボタンIDを返すようにしようとしています:戻るボタンID発行jqueryの

$(document).on('click', 'button.btn.btn-primary', function() { 
    console.log(this.id); 
}); 

しかし、これはundefinedを返します。 console.log(this)を試したところ、ウィンドウオブジェクトが戻ってきました。私はidを返す私の他のボタンでこの動作を参照してください..誰かが助けることができますか?

<tr class="child"> 
    <td class="child" colspan="8"> 
     <li data-dtr-offset-index-"7"> 
      <span class="dtr-title"></span> 
      <span class="dtr-data"> 
       <button id="{{product_code}}" type="button" class="btn btn-primary">Add to Cart</button> 
      </span> 
     </li> 
    </td> 
</tr> 

追加コンテキスト:私はjqueryののDataTableを使用しています

は、ここに私のhtmlです。上記のhtmlは、表が応答モードのときに表示されます。 ありがとうございます!

+2

'button'にはidがありません。 – Azim

+0

申し訳ありませんが、間違ったHTMLをコピーしました。私はただそれを更新しました。 –

+0

あなたは 'angularjs'を使っていますか? – Azim

答えて

1

ボタンが

が存在するので、構築のすべての投稿をjsfiddleの例ではなく、in head

$(function() { 
 
    $(document).on('click', 'button.btn.btn-primary', function() { 
 
    console.log(this.id); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<tr class="child"> 
 
    <td class="child" colspan="8"> 
 
    <li data-dtr-offset-index- "7"> 
 
     <span class="dtr-title"></span> 
 
     <span class="dtr-data"> 
 
       <button id="{{product_code}}" type="button" class="btn btn-primary">Add to Cart</button> 
 
      </span> 
 
    </li> 
 
    </td> 
 
</tr>

のデフォルト onloadを持っていることに注意してください
0

jQueryソリューションを試しましたか?あなたはオンロードでそれを追加する必要があり

$(document).on('click', 'button.btn.btn-primary', function() { 
    console.log($(this).attr('id')); 
}); 
+0

私はちょうど試しました。それはまた 'undefined'を返しています –

0

https://jsfiddle.net/yt22yq8h/

$(document).on('click', 'button.btn.btn-primary', function() { 
    console.log(this.id); 
}); 

これは私にとってうまくいきます。上記のfiddleを参照してください。これはコンソールにIDを表示します。

+0

JSFiddleは' onload'デフォルトを使用しているので – mplungjan

関連する問題