1
$("#showKey").each(
    $(this).click(function(){
        alert($(this).attr("value"));
    })
);

<a id="showKey" href="#" value="{{ customer.key }}">
   <span class="icons icon-key"></span>
   Show key
</a>

アラートは未定義の出力を提供します。「未定義」だけです。顧客のリストがあり、#showKeyをクリックすると、クリックした顧客のキーが表示されます。

私のコードの何が問題になっていますか?

4

4 に答える 4

6

同じ ID を持つ複数の要素を持つことはできません。代わりにクラスを使用してください。さらに、.each を呼び出す必要はありません。代わりにクラス セレクターを使用してください。

$(".showKey").click(function(){
     alert($(this).data("key"));
);

<a class="showKey" href="#" data-key="{{ customer.key }}">Show key</a>
于 2012-05-10T20:12:38.743 に答える
3

属性を使用できdataます:

<a id="showKey" href="#" data-value="{{ customer.key }}">
   <span class="icons icon-key"></span>
   Show key
</a>


$("#showKey").click(function(){
    alert($(this).data("value"));
})

http://jsfiddle.net/LKArX/

于 2012-05-10T20:13:12.520 に答える
1

You do not need the jQuery each function.

$("#showKey").click(function(){
   alert($(this).attr("value"));
});
于 2012-05-10T20:16:32.643 に答える
0

The problem with your code is in your usage of

$("#showkey").each({...});

You should simply use

$("#showkey").click({function(){
   alert( $this).val() );
   }
});

to bind the click event to every showkey id'd element.

于 2012-05-10T20:17:06.140 に答える