Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery文本字段小计数量_Jquery_Asp.net - Fatal编程技术网

jquery文本字段小计数量

jquery文本字段小计数量,jquery,asp.net,Jquery,Asp.net,我正在尝试编写一个jquery脚本,以将ID中包含“*Amount”的所有文本字段相加。我有以下内容,但不知道如何将它们相加并将值分配给其他文本框 <input type="text" id="1stAmount"> <input type="text" id="2ndAmount"> <input type="text" id="3rdAmount"> // to get all the values of the textboxes that have

我正在尝试编写一个jquery脚本,以将ID中包含“*Amount”的所有文本字段相加。我有以下内容,但不知道如何将它们相加并将值分配给其他文本框

<input type="text" id="1stAmount">
<input type="text" id="2ndAmount">
<input type="text" id="3rdAmount">

// to get all the values of the textboxes that have Amount in the ID
$( "input[name*='Amount']" ).value();

//获取ID中包含金额的文本框的所有值
$(“输入[name*='Amount']”)value();

您不能使用
[name*=Amount]
获取带有id的文本框,请使用
[id*=Amount]

使用

var ids = $('[id*=Amount]');
var count = 0;
$.each(ids,function(){
    count += parseFloat(this.value);
});
alert(count);
试试这个:

    var values =  $( "input[name*='Amount']" ).value();
var sum = 0;
for (int i=0;i<values.length;i++){
sum += parseInt(values[i]);
}
$("#yourTextbox").val(sum);
var values=$(“输入[name*='Amount']”)value();
var总和=0;

对于(int i=0;i您可以使用“[id*=Amount]”获取id与
Amount
匹配的所有输入。此外,
.val()
是检索输入值的方法。但是,这仅检索第一个匹配的值:

$('[id*=Amount]').val();  // will be the first input's value
要检索所有值,必须循环遍历返回的集合中的每个元素,并将值相加:

var total = 0;
$('[id*=Amount]').each(function(element) {
  // get the value of the current element
  var text = $(this).val();
  // add the parsed total
  total += parseFloat(text );
});

// do something with total here
alert(total)
示例JsFiddle at

为什么将其标记为C#?