1

当我单击减号按钮时,我希望文本框中的值减少,当我单击加号按钮时,我希望文本框中的值增加。我正在使用一个 jQuery 模板,它接受 JSON 来自动填充。

<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 事件添加到标记中的按钮,我该如何实现我的目标?

4

1 回答 1

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' = '减号按钮';
  • 'valueContainer' = 你用来容纳数量的跨度。
于 2012-04-13T20:59:24.493 回答