繁体   English   中英

使用jQuery的具有相同名称但不同ID的多个输入字段的总和

[英]Sum of multiple input fields with same name but not same id using jQuery

我正在尝试制作一个 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">

注意:我使用带有 jQ​​uery 功能的按钮在按下时添加新字段,正在使用appendTo添加更多字段。 有时我有或多或少的字段,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.

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