需要更改输入名称以输入ID

时间:2018-08-05 23:06:00

标签: javascript jquery

我需要将计算从输入名称更改为输入ID。 原来的输入名称是“ ef-fee”。 新的ID为“ ef-fee _” + [i]);

使用输入名称的旧代码:

<script async> //Entrance Fees 

function findTotalEF(){
var arrEF = document.getElementsByName('ef-fee');
var totEF=0;

for(var i=0; i<arrEF.length; i++){


if(parseInt(arrEF[i].value)) totEF += parseInt(arrEF[i].value);}
document.getElementById('totalEF').value = totEF;
}

这是无效的新代码:

<script async> //Entrance Fees 

function findTotalEF(){

var totEF=0;    

for (i=0; i <=48; i++) 


if ($('#ef-fee_' + i > 0)) {totEF += "ef-fee_"  +[i].val;} // This line is not correct. Can someone please fix it?

document.getElementById('totalEF').value = totEF;

}

1 个答案:

答案 0 :(得分:0)

应该做什么你需要的

function findTotalEF() {
  var totEF = 0;
  for (var i = 0; i <= 48; i++) {
    var $input = $('#ef-fee_' + i);
    var value =  parseInt($input.val()) || 0;
    totEF += value > 0 ? value : 0; 
  }
  $('#totalEF').val(totEF);
}
相关问题