jQuery Template を使用して情報のテーブルを作成しています。各行には、マイナス ボタン、プラス ボタン、およびそれらの間のテキスト ボックスが含まれます。一部テキストもあります。マイナスまたはプラス ボタンをクリックすると、テキスト ボックス内の数値が増減します。これらはすべてテンプレートを介して動的にレンダリングされるため、ボタンはどのように機能しますか? テストとして次のことを試しましたが、すべてのマイナスボタン要素でクリック関数を呼び出すだけです:
jQuery('#template').tmpl(data).appendTo("#holdingCell");
jQuery('#holdingCell #minusbutton').click(
function(){
alert(this.id);
});
これが私のコードの関連部分です。これを機能させる方法について誰か提案がありますか?
<script id="template" type="text/x-jquery-tmpl">
<div style="display:block;margin-bottom:20px;" id="${getNextId()}">
<div class="adjuster"><button id='minusbutton'>-</button><input id='quantityText' class="enterQuantity" type=text style="width:40px; margin-left:5px;margin-right:5px" /><button id=">+</button></div>
<div class="productName" style="width:200px"><div>${productname}</div></div>
<div class="quantity"><span style="font-weight:bold;">${quantity}</span><span> Remaining</span></div>
</div>
function getNextId(){
return id++;
}
function buildDialog(json){
//Stuff I didn't paste here turns the parameter 'json' into a variable 'data'
jQuery('#template').tmpl(data).appendTo("#holdingCell");
jQuery('#holdingCell #minusbutton').click(
function(){
alert(this.id);
});