Jquery计算

Jquery计算,jquery,Jquery,我正在使用jquery计算在我的一个客户端页面上进行求和。简单的计算很好,但我必须集成一个添加和删除按钮和输入标签。与手动输入数字不同,客户希望使用“添加”和“删除”按钮来增加和减少乘积的值,并最终累加到总和 希望有人能帮我,因为我还不是jquery方面的专家。:) 这里是HTML代码 <div class="amount"> <input type="text" class="count" name="totalSum" id="totalS

我正在使用jquery计算在我的一个客户端页面上进行求和。简单的计算很好,但我必须集成一个添加和删除按钮和输入标签。与手动输入数字不同,客户希望使用“添加”和“删除”按钮来增加和减少乘积的值,并最终累加到总和

希望有人能帮我,因为我还不是jquery方面的专家。:)

这里是HTML代码

<div class="amount">    
            <input type="text" class="count"  name="totalSum" id="totalSum" value="" size="2" readonly="readonly">                      </div>



    <div class="clearfix"></div><!--Please do not delete this div-->
    <!--Case 1-->       
    <div class="bottle_in_case">
                <div class="addRemove">
                <input type="text" value="2" class="count" maxlength="2" name="case" id="case_1"> bottles
                <p class="add_remove_btns"><a href="#"><img src="images/add.gif" alt=""> Add</a> / <a href="#"><img src="images/remove.gif" alt=""> Remove</a></p>
            </div>
    </div>

    <!--Case 2-->       
    <div class="bottle_in_case">

        <div class="addRemove">
                <input type="text" value="2" class="count" maxlength="2" name="case" id="case_2"> bottles
                <p class="add_remove_btns"><a href="#"><img src="images/add.gif" alt=""> Add</a> / <a href="#"><img src="images/remove.gif" alt=""> Remove</a></p>
            </div>
    </div>

    <!--Case 3-->       
    <div class="bottle_in_case last">

            <div class="addRemove">
                <input type="text" value="2" class="count" maxlength="2" name="case" id="case_3"> bottles
                <p class="add_remove_btns"><a href="#"><img src="images/add.gif" alt=""> Add</a> / <a href="#"><img src="images/remove.gif" alt=""> Remove</a></p>
            </div>
    </div>
$(“input[name^=case]”)。sum(“keyup”、“totalSum”)

提前谢谢


Neha

使用jquery进行简单计算

    <table id="table" border="0">
        <thead>
            <th>Price</th><th>Quantity</th><th>Total</th>
        </thead>
    <tbody>
    <tr>
        <td><input id ="price" type = "text"></input></td>
        <td><input id ="quantity" type = "text"></input></td>
        <td><input id ="total" type = "text"></input></td>
    </tr>
    </tbody>
    </table>

<a href="#" id="calc">Calculate</a>

我希望它能对您有所帮助

首先:将数量转换器连接到add.gif和remove.gif:

<p class="add_remove_btns"><a href="#"><img src="images/add.gif" alt=""> Add</a> / <a href="#"><img src="images/remove.gif" alt=""> Remove</a>

让我知道这是否有效。。也为我提供一个URL,以便在出现错误时进行故障排除

你是说JavaScript吗?你想知道如何使用jquery获取值吗?我被要求使用jquery,但是如果JavaScript中的一些简单的东西可以帮上忙,那为什么不呢。只是我对JS不是很熟悉!看看这里的页面:我正在使用jquery.calculation获得一个简单的totalSum,它工作得很好,但是现在我必须将这个add和remove按钮与输入集成,以增加和减少总和!嘿,谢谢。。我用了一些类似的方法来做这个网站上的计算,也别忘了包括一个方法来运行recalculate_total()somewher-可能是我在最后一个代码框中编写的单击函数的一部分。嘿,你能看看代码吗。我在问题中编辑了它。对不起打扰你了。如果你能看一看,那就太好了。谢谢。好吧,我忘了*1。var newqty=$('#case"+target).val()+1;应该是var newqty=$('#case"+target).val()*1+1;移除函数也是如此。试试看……如果不将值指定为整数,它将作为字符串传递,1+1变为11,而不是2。但是,将一个值乘以1(*1)是将一个字符串数字转换为真正整数的可靠方法。也适用于PHP设置。但是总计仍然不正确:(((…已更新页面..您可以再次检查..当您减去它时,它仍会继续添加到总计中!!
$(function(){
    $('a#calc').click(function(){
    var q = $('input#quantity').val();
    var p = $('input#price').val();
    var tot = (q*p);
    $('input#total').val(tot);
    });
 });
<p class="add_remove_btns"><a href="#"><img src="images/add.gif" alt=""> Add</a> / <a href="#"><img src="images/remove.gif" alt=""> Remove</a>
<p class="add_remove_btns"><a href="#" class="add_qty" rel="1"><img src="images/add.gif" alt=""> Add</a> / <a href="#" class="remove_qty" rel="1"><img src="images/remove.gif" alt=""> Remove</a>
$('add_qty').each(function(){
  $(this).click(function() {
    var target = $(this).attr('rel');
    var newqty = $('#case_'+target).val()+1;
    $('#case_'+target).val(newqty);
    recalculate_total();
    return false;
  });    
});

$('remove_qty').each(function(){
  $(this).click(function() {
    var target = $(this).attr('rel');
    var newqty = $('#case_'+target).val()-1;
    if(newqty<0) {newqty=0;} // qty cannot be below 0, so override to 0 if thats the case
    $('#case_'+target).val(newqty);
    recalculate_total();
    return false;
  });    
});
 function recalculate_total() {
    var total = 0;
    $('input.count').each(function(){
    total = total+$(this).val()*1;
    });
    $('#totalSum').val(total);
    }