简体   繁体   English

如何在我的输入值和更改中添加总和

[英]How to add sum in my input value and chage

I have this code:我有这个代码:

<input id=​"InvoiceItemPrice:​:​:​:​" name=​"InvoiceItemPrice:​:​:​:​" value size=​"6">​
<input id=​"InvoiceItemPrice:​:​1:​:​" name=​"InvoiceItemPrice:​:​1:​:​" value=​"46.00" size=​"6">​
<input id=​"InvoiceItemPrice:​:​2:​:​" name=​"InvoiceItemPrice:​:​2:​:​" value=​"79.00" size=​"6">​, 
<input id=​"InvoiceItemPrice:​:​3:​:​" name=​"InvoiceItemPrice:​:​3:​:​" value=​"14.00" size=​"6">​

I like add sum in every value ot input type i have this code:我喜欢在输入类型的每个值中添加 sum 我有这个代码:

var sum = 100/123;
$('input[name="InvoiceItemPrice::2::"]').each(function()
{
    sum *= parseFloat($(this).val());
});
$('input[name="InvoiceItemPrice::2::"]').val(sum);

How to change all value on all input.如何更改所有输入的所有值。

I tried with this $("input [name = ' * InvoiceItemPrice ']") but I caught the first that does not have a value and NaN gives me an error.我试过这个$("input [name = ' * InvoiceItemPrice ']")但我发现了第一个没有值的NaN给了我一个错误。

What is happening is your sum variable is getting re-used in your each statement, if you change that line inside of your each you should be A-ok.发生的事情是您的 sum 变量在您的 each 语句中被重新使用,如果您更改 each 内的那一行,您应该没问题。

I have created a jsFiddle here https://jsfiddle.net/r79d3121/3/ which may help我在这里创建了一个 jsFiddle https://jsfiddle.net/r79d3121/3/这可能会有所帮助

HTML HTML

<input id="InvoiceItemPrice::::" name="InvoiceItemPrice::::" value size="6">
<input id="InvoiceItemPrice::1::" name="InvoiceItemPrice::1::" value="46.00" size="6">
<input id="InvoiceItemPrice::2::" name="InvoiceItemPrice::2::" value="79.00" size="6">,
<input id="InvoiceItemPrice::3::" name="InvoiceItemPrice::3::" value="14.00" size="6">

jQuery jQuery

var sum = parseFloat(100 / 123);
$(function () {
    var result = 0;
    $('[id*="InvoiceItemPrice"]').each(function () {
        $(this).val(parseFloat((parseFloat($(this).val()) * parseFloat(sum))));
    });
});

Updated to change only the value from the input selected更新为仅更改所选输入中的值

2nd Update, values are calculated when the document is ready, if you want the values to be updated you could always check for .focusout() on each input and then run the code to calculate the value第二次更新,在文档准备好时计算值,如果您希望更新值,您可以始终在每个输入上检查.focusout() ,然后运行代码来计算值

3rd Update, the html is back to the original and also, you can just user a partial selector in your jQuery which I have provided and updated the jsFiddle link第 3 次更新,html 恢复到原来的状态,而且,您可以在您的 jQuery 中使用部分选择器,我提供并更新了 jsFiddle 链接

Try something like this:尝试这样的事情:

$('input[name^=InvoiceItemPrice]').each(function() {
$(this).val("example");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM