Javascript 在计算输入类型编号的金额时遇到问题

Javascript 在计算输入类型编号的金额时遇到问题,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我使用类编写了jquery代码, 问题是,即使我只为一个产品做了更改,每个产品都会增加。 我想做的是,如果用户增加一个字段,而不是他父文本框中计算的金额,但实际上,当我增加一个产品的数量时,两个产品的数量都会增加 $(文档).ready(函数(){ $(“.quantity”).change(函数(){ $(“.subTotal”).val(parseInt($(this.val())*parseInt($(“.price”).val()); }); }); 基于事件触发元素获取输入元素。使

我使用类编写了jquery代码, 问题是,即使我只为一个产品做了更改,每个产品都会增加。 我想做的是,如果用户增加一个字段,而不是他父文本框中计算的金额,但实际上,当我增加一个产品的数量时,两个产品的数量都会增加


$(文档).ready(函数(){
$(“.quantity”).change(函数(){
$(“.subTotal”).val(parseInt($(this.val())*parseInt($(“.price”).val());
});
});

基于事件触发元素获取输入元素。使用方法获取输入

$(document).ready(function() {
  $(".quantity").change(function() {
    // cache the `$(this)` jQuery object
    var $this = $(this);
    $this
      // get all siblings next to it with class `subTotal`
      .nextAll(".subTotal")
      // get the adjucent one
      // .nextAll(".subTotal").first() ==> .nextAll(".subTotal:first")
      .first()
      // updates its value
      .val(
        // multiply with adjucent pric input 
        ($this.val() * $this.nextAll(".price").first().val()) || 0
      );
  });
});
$(文档).ready(函数(){
$(“.quantity”).change(函数(){
var$this=$(this);
$this.nextAll(“.subTotal”).first()
.val(($this.val()*$this.nextAll(“.price”).first().val())| | 0);
});
});

基于事件触发元素获取输入元素。使用方法获取输入

$(document).ready(function() {
  $(".quantity").change(function() {
    // cache the `$(this)` jQuery object
    var $this = $(this);
    $this
      // get all siblings next to it with class `subTotal`
      .nextAll(".subTotal")
      // get the adjucent one
      // .nextAll(".subTotal").first() ==> .nextAll(".subTotal:first")
      .first()
      // updates its value
      .val(
        // multiply with adjucent pric input 
        ($this.val() * $this.nextAll(".price").first().val()) || 0
      );
  });
});
$(文档).ready(函数(){
$(“.quantity”).change(函数(){
var$this=$(this);
$this.nextAll(“.subTotal”).first()
.val(($this.val()*$this.nextAll(“.price”).first().val())| | 0);
});
});

如果只允许使用类,您可以尝试将每对输入包装在一个范围内,这些输入的类/ID从1到3。然后,在onchange函数中,您可以获取该元素的父元素,并循环遍历所有.price元素,直到获得与该元素具有相同父节点的元素。现在知道了您想要影响的元素,您现在可以轻松地获取/设置所述元素中的文本。

如果您只允许使用类,您可以尝试将每对输入包装在一个范围内,这些输入的类/ID从1到3。然后,在onchange函数中,您可以获取该元素的父元素,并循环遍历所有.price元素,直到获得与该元素具有相同父节点的元素。现在知道了要影响的元素,您现在可以轻松地在所述元素中获取/设置文本。

$(函数(){
$(“.quantity”).change(函数(){
$(this.closest('tr').find('.subTotal').val(parseInt($(this.val())*parseInt($(this.closest('tr').find('.price').val());
});
});
/*
在编辑问题之前
$(函数(){
$(“.quantity”).change(函数(){
$(this.nextAll(“.subTotal:first”).val(parseInt($(this.val())*parseInt($(this.nextAll('.price:first').val());
});
});
*/

$(函数(){
$(“.quantity”).change(函数(){
$(this.closest('tr').find('.subTotal').val(parseInt($(this.val())*parseInt($(this.closest('tr').find('.price').val());
});
});
/*
在编辑问题之前
$(函数(){
$(“.quantity”).change(函数(){
$(this.nextAll(“.subTotal:first”).val(parseInt($(this.val())*parseInt($(this.nextAll('.price:first').val());
});
});
*/


@KaushalShah您是否完全理解,或者您需要任何意见?非常感谢您,先生。。。!!您好,先生,我已经检查了我的脚本,发现它不起作用。我的脚本有一个表,这段代码不能像aspected那样工作。我需要看看你的html。请编辑问题。完成。让我知道。@KaushalShah您是否完全理解,或者您需要任何意见?非常感谢您,先生。。。!!您好,先生,我已经检查了我的脚本,发现它不起作用。我的脚本有一个表,这段代码不能像aspected那样工作。我需要看看你的html。请编辑问题。完成。让我知道。