微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 如何根据输入类型编号总结值* $(value)

JS:

var quantity = $("#quantity").val();
var ticketprice = $("#ticketprice").val();
var total = ticketprice * quantity;

$(document).ready(function (e) {
  $("input").change(function () {
    var value = 0;
    $("input[name=quantity]").each(function () {
        value = value + parseInt($(this).val());
    }),$("input[name=jumlah3]").val(total);
  });
});

数量在此JS文件中的JQuery上运行:

function wcqib_refresh_quantity_increments() {
        jQuery("div.quantity:not(.buttons_added),td.quantity:not(.buttons_added)").each(function (a,b) {
            var c = jQuery(b);
            c.addClass("buttons_added"),c.children().first().before('<input type="button" value="-" class="minus" />'),c.children().last().after('<input type="button" value="+" class="plus" />');
        });
    }
    String.prototype.getDecimals || (String.prototype.getDecimals = function () {
        var a = this,b = ("" + a).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
        return b ? Math.max(0,(b[1] ? b[1].length : 0) - (b[2] ? +b[2] : 0)) : 0;
    }),jQuery(document).ready(function () {
        wcqib_refresh_quantity_increments();
    }),jQuery(document).on("updated_wc_div",function () {
        wcqib_refresh_quantity_increments();
    }),jQuery(document).on("click",".plus,.minus",function () {
        var a = jQuery(this).closest(".quantity").find(".qty"),b = parseFloat(a.val()),c = parseFloat(a.attr("max")),d = parseFloat(a.attr("min")),e = a.attr("step");
        b && "" !== b && "NaN" !== b || (b = 0),"" !== c && "NaN" !== c || (c = ""),"" !== d && "NaN" !== d || (d = 0),"any" !== e && "" !== e && void 0 !== e && "NaN" !== parseFloat(e) || (e = 1),jQuery(this).is(".plus") ? c && b >= c ? a.val(c) : a.val((b + parseFloat(e)).toFixed(e.getDecimals())) : d && b <= d ? a.val(d) : b > 0 && a.val((b - parseFloat(e)).toFixed(e.getDecimals())),a.trigger("change");
    });

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ui-grid-a">
    <div class="quantity buttons_added">
        <table class="registration-form" id="registration-form">
            <tr>
                <td>
                    <div class="ui-block-a">
                        <div id="test">
                            <input type="button" value="-" class="minus" id="jumlah" name="jumlah">
                        </div>
                    </div>

                    <div class="ui-block-b">

                        <input type="number" step="1" min="1" max="10" id="quantity" name="quantity" value="1" title="Qty" class="input-text qty text" size="6" pattern="" inputmode="" readonly>

                    </div>

                    <div class="ui-block-c">
                        <div id="test2">
                            <input type="button" value="+" class="plus" id="jumlah2" name="jumlah2">
                        </div>
                    </div>


                </td>
            </tr>


        </table>


        <div id="test3">
            <input type="text" value="0" id="jumlah3" name="jumlah3" readonly> 
        </div>

    </div>
</div>

如何自动汇总输入值?

目前,当我增加数量时,显示的总价格值不会改变并保持为票价* 1.

因此,例如,如果1个数量的票价是50美元,当我更改为2个数量时,它仍然是50美元而不是100美元.

解决方法

试试这个:

如果票价为100:

$(document).ready(function(){
  var ticketprice = 100 ;
  var quantity ;
  $('input[type=button]').click(function(){
    quantity = parseInt( $("#quantity").val() ) ;
    if(this.className == 'minus' )
      quantity = (quantity - 1) < 0 ? 0 : (quantity - 1) ;
    else if (this.className == 'plus')
      quantity = quantity + 1 ;
    $("#quantity").val(quantity);
    $("input[name=jumlah3]").val( quantity * ticketprice ) ;
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="ui-grid-a">
  <div class="quantity buttons_added">
    <table class="registration-form" id="registration-form">
      <tr>
        <td>
          <div class="ui-block-a">
            <div id="test">
              <input type="button" value="-" class="minus" id="jumlah" name="jumlah">
            </div>
          </div>
          <div class="ui-block-b">
            <input type="number" step="1" min="1" max="10" id="quantity" name="quantity" value="1" title="Qty" class="input-text qty text" size="6" pattern="" inputmode="" readonly>
          </div>
          <div class="ui-block-c">
            <div id="test2">
              <input type="button" value="+" class="plus" id="jumlah2" name="jumlah2">
            </div>
          </div>
        </td>
      </tr>
    </table>
    <div id="test3">
      <input type="text" value="100" id="jumlah3" name="jumlah3" readonly> 
    </div>
  </div>
</div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐