这段代码有什么问题......?

时间:2014-09-30 10:41:35

标签: javascript html html5

我希望当前价格能够汇总最后一列的所有价格,这是我的代码..我应该在最后一列添加什么 这是我的HTML



<input type="text" name="quantity" id="quantity" />
<input type="text" name="price" id="price" placeholder="Enter Price"/>
<br/>
<center>
<input type="button" id="add" name="add" value="Add to sales order" />
</center>
<span id="totalPrice">0</span>
<div class="messages"  align="left" style="float:left; margin: 40px;">
<br/><br/>
</div>
<div class="messages1"  style="float:left; margin: 40px;"> <br/><br/></div>
<div class="messages2"  style="float:left; margin: 40px;"><br/> <br/></div>
<div class="messages3"  style="float:left; margin: 40px;"><br/> <br/></div>
<div class="messages4"  style="float:left; margin: 40px;"> <br/><br/></div>
<div class="messages5"  style="float:left; margin: 40px;"><br/> <br/></div>
<div class="messages6"  style="float:left; margin: 40px;"><br/> <br/></div>
<div class="messages7"  style="float:left; margin: 40px;"><br/> <br/></div>
&#13;
&#13;
&#13;

这是我的jquery,我想要的价格是我应该做的总价格

&#13;
&#13;
$(document).ready(function() {
    $('#add').click(function() {
  //var toAdd = $("select[name=category]").val();
         var category = $("#category option:selected").text();
       $('.messages').append("<p>"+category+"</p>");
      
      var brand = $("#brand option:selected").text();
       $('.messages1').append("<p>"+brand+"</p>");
       var field = $("#field option:selected").text();
       $('.messages2').append("<p>"+field+"</p>");
       var quality = $("#quality option:selected").text();
       $('.messages3').append("<p>"+quality+"</p>");
       var product = $("#product option:selected").text();
       $('.messages4').append("<p>"+product+"</p>");
       
       var quantity = $("#quantity").val();
       $('.messages5').append("<p>"+quantity+"</p>");
       
       var price = $("#price").val();
       $('.messages6').append("<p>"+parseFloat(quantity) * parseFloat(price)+"</p>");
      
     
var currentPrice =  parseFloat(price);
   
         var value = price; 
        currentPrice = parseFloat(currentPrice) + value;                                  
        $('span').text(currentPrice);
 
});
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

使用as,你的js函数需要一些改进,如下所示:

$(document).ready(function() {
    var currentPrice =  0;
    $('#add').click(function() {
  //var toAdd = $("select[name=category]").val();
         var category = $("#category option:selected").text();
       $('.messages').append("<p>"+category+"</p>");

      var brand = $("#brand option:selected").text();
       $('.messages1').append("<p>"+brand+"</p>");
       var field = $("#field option:selected").text();
       $('.messages2').append("<p>"+field+"</p>");
       var quality = $("#quality option:selected").text();
       $('.messages3').append("<p>"+quality+"</p>");
       var product = $("#product option:selected").text();
       $('.messages4').append("<p>"+product+"</p>");

       var quantity = $("#quantity").val();
       $('.messages5').append("<p>"+quantity+"</p>");

       var price = $("#price").val();
       $('.messages6').append("<p>"+parseFloat(quantity) * parseFloat(price)+"</p>");




         var value = price; 
        currentPrice = parseFloat(currentPrice) + value*quantity; 
        $('span').text(currentPrice);

});
});

DEMO