jQuery将字段更改为相关字段的子字符串

jQuery将字段更改为相关字段的子字符串,jquery,field,substring,Jquery,Field,Substring,另一个jquery计算问题 我有这个,这是我正在玩的插件站点的示例代码,以使其工作: function recalc(){ $("[id^=total_item]").calc( "qty * price", { qty: $("input[name^=qty_item_]"), price: $("input[name^=price_item_]"), }, functi

另一个jquery计算问题

我有这个,这是我正在玩的插件站点的示例代码,以使其工作:

        function recalc(){
    $("[id^=total_item]").calc(
        "qty * price",
        {
        qty: $("input[name^=qty_item_]"), 
        price: $("input[name^=price_item_]"),
        },

        function (s){ return s.toFixed(2);},

        function ($this){ var sum = $this.sum();

            $("#grandTotal").val(
                // round the results to 2 digits
                sum.toFixed(2)
            );
        }
    );
}
价格字段中的更改会导致总计更新:

        $("input[name^=price_item_]").bind("keyup", recalc);
        recalc();
问题是我不知道price字段的值,它们只能作为用户输入的值的子字符串,称之为“itemcode”。根据php查询,项目的数量将是可变的

我提出了以下方法来根据项目代码更改价格:

    $("input[name^='itemcode_item_1']").keyup(function () {
    var codeprice = this.value.substring(2,6);
    $("input[name^='price_item_1']").val(codeprice);
});

$("input[name^='itemcode_item_2']").keyup(function () {
    var codeprice = this.value.substring(2,6);
    $("input[name^='price_item_2']").val(codeprice);
});
但是,在这样做的同时,它也会阻止项目_total的更新。另外,我觉得必须有一种方法,不需要为列表中的每一项编写编号函数。然而,当我只是使用

    $("input[name^='itemcode_item_']")
更新任何itemcode字段都会更新所有价格字段,这是不好的

谁能给我指出正确的方向吗?我知道我在这里有点不知所措,但我不喜欢javascript

好的,现在更新它以添加一些html。基本上,这只是jquery calculator站点中的一小部分示例,我正在使用它来获得功能,然后再将其全部放入实际的功能脚本中。实际上,项目的数量是可变的,它们将以各种方式进行分组

<table>

            <tr><th>Qty</th><th align="left"></th><th>Item code</th><th>Price</th><th>Total</th></tr>
            <tr>
            <td><input type="text" name="qty_item_1" id="qty_item_1" value="1" size="3"/></td>
            <td>Item One</td>
            <td><input type="text" id="itemcode_item_1" name="itemcode_item_1" value="" size="8" /></td>
            <td><input type="text" id="price_item_1" name="price_item_1" value="" size="8" /></td>
            <td><input type="text" id="total_item_1" value=" " size="8"  readonly="readonly"  /></td>

            </tr>

            <tr>
            <td><input type="text" name="qty_item_2" id="qty_item_2" value="1" size="3"/></td>
            <td>Item Two</td>
            <td><input type="text" id="itemcode_item_2" name="itemcode_item_2" value="" size="8" /></td>
            <td><input type="text" id="price_item_2" name="price_item_2" value="" size="8" /></td>
            <td><input type="text" id="total_item_2" value=" " size="8" readonly="readonly" /></td>
            </tr>
            <tr>
            <td colspan="4" align="right"><strong>Grand Total:</strong></td>
            <td><input type="text" id="grandTotal" value=" " size="6" readonly="readonly">    </td>
            </tr>
        </table>

QtyItem代码价格总计
项目一
项目二
总计:

我已经解决了第二个问题(总数未更新),这是我以前应该想到的:

        $("input[name^=itemcode_item_]").bind("keyup", recalc);
        recalc();

现在介绍如何在不重复每个编号项目的功能的情况下执行此操作。

您写道,元素将以各种方式分组,但如果始终保持此模式(即相关字段位于一行内):

一般来说,试着找出
itemcode\u item\u
字段与其对应的
price\u item\u
字段之间的关系。

在这种情况下,它们有一个共同的祖先
tr
,并且其中只有on
price\u item
输入字段。另一个关系是,
price\u item\u 1
itemcode\u item\u 1
s父项的下一个兄弟的第一个子项。

如果您能提供一些示例HTML,那就太好了。也许我们可以找到另一个jQuery选择器组合。另外,为什么它会停止recalc功能的工作?我是对的,你的基本问题是,从其他值(作为子字符串)获取更新的价格值并重新计算总和?但是说实话,我发现用户能够输入一些包含价格的“项目代码”是非常奇怪的。这是真的吗?作为一个用户,我可以输入任何我喜欢的价格,还是我错了?这不是一个公共页面,只有一个人会使用它。它是从条形码扫描器中提取项目代码。正如我在评论中所说的,如果你展示一些HTML代码示例,我们可能会找到一些方法。但这取决于结构;)天哪,这太聪明了!!非常感谢你,它就像一个符咒。是 啊
<tr>
  <td><input type="text" id="itemcode_item_1" name="itemcode_item_1" value="" size="8" /></td>
  <td><input type="text" id="price_item_1" name="price_item_1" value="" size="8" /></td>
</tr>
$("input[name^='itemcode_item_']").keyup(function () {
    var codeprice = this.value.substring(2,6);
   // closest(sel) finds the closest ancestor that matches 'sel'
   // find(sel) finds  all descendants that match the selector 'sel'
   $(this).closest('tr').find("input[name^='price_item_']").val(codeprice);
});