2012-04-13 42 views
1

當我單擊減號按鈕時,我希望文本框內的值減少,當我單擊加號按鈕時,我希望文本框內的值增加。我正在使用JSON模板來自動填充JSON。需要幫助將事件綁定到jQuery模板

<script id="template" type="text/x-jquery-tmpl"> 
    <div style="display:block;" 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='plusbutton'>+</buton> 
    </div> 
    <div class="productName"> 
     <div>${productname}</div> 
    </div> 
    <div class="quantity"> 
     <span style="font-weight:bold;">${quantity}</span> 
     <span> Remaining</span> 
    </div> 
    </div> 
</script> 

如果我在標記中的按鈕上添加onclick事件,我該如何實現我的目標?

回答

0

這裏有一個簡單的實現:http://jsfiddle.net/GAfyW/4/

HTML:

<div id="plusOne">Click to Increment</div> 
<div id="minusOne">Click to Decrement</div> 
<span type="text" id="valueContainer"/>0</span> 

JS放置的onload塊中:

$('#plusOne').bind('click', function() { 
    var value = $('#valueContainer').html(); 
    // Increment the value 
    $('#valueContainer').html(parseInt(value)+1); 
}); 

$('#minusOne').bind('click', function() { 
    var value = $('#valueContainer').html(); 
    // Decrement the value 
    $('#valueContainer').html(parseInt(value) - 1); 
}); 

在這裏:

  • 'Plusone精選'=' plusButton「;
  • 'minusOne'='minusButton';
  • 'valueContainer'=您用來存放數量的跨度。
+0

文本輸入實現:[jsFiddle](http://jsfiddle.net/GAfyW/2/) – aztechy 2012-04-13 21:01:16