[英]Sum of multiple input fields with same name but not same id using jQuery
I am trying to make an jQuery function to make sum of some input fields with array name, but not same id.我正在尝试制作一个 jQuery 函数来对一些具有数组名称但不相同 id 的输入字段进行求和。
<input name="ingredient[1]" type="text" id="autofield_1" class="ingrediente">
<input name="ingredient[2]" type="text" id="autofield_2" class="ingrediente">
<input name="ingredient[3]" type="text" id="autofield_3" class="ingrediente">
<input name="ingredient[4]" type="text" id="autofield_4" class="ingrediente">
<input name="ingredient[5]" type="text" id="autofield_5" class="ingrediente">
<input name="ingredient[6]" type="text" id="autofield_6" class="ingrediente">
Note: I use a button with jQuery function to add new field on press, is using appendTo
to add more fields.注意:我使用带有 jQuery 功能的按钮在按下时添加新字段,正在使用
appendTo
添加更多字段。 sometimes I have more or less fields, the jQuery function must be dynamic, to apply on the number of input fields set.有时我有或多或少的字段,jQuery 函数必须是动态的,以应用设置的输入字段数量。
var sum = 0;
// or $( 'input[name^="ingredient"]' )
$( '.ingrediente' ).each( function( i , e ) {
var v = parseInt( $( e ).val() );
if ( !isNaN( v ) )
sum += v;
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.