gpt4 book ai didi

javascript - 如何乘或加动态 javascript 输入字段的值

转载 作者:行者123 更新时间:2023-12-02 22:49:02 25 4
gpt4 key购买 nike

输入字段由javascript生成。无论我生成多少个输入字段,我都想选择每个值...这样我就可以使用这些值进行一些计算。

<form action="{{ route('addmorePost') }}" method="POST">
<div class="table-responsive">
<span id="result"></span>

<table class="table table-bordered">
<thead>
<tr>
<th width="55%">Sales Representative</th>
<th width="15%">Target per day</th>
<th width="15%">Monthly day</th>
<th width="10%"> <a href="#" class="btn btn-info addRow">+ </a> </th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="target[]" class="form-control target" id="target" oninput="calc()">
</td>

<td><span id="sum1" onchange="add()">0</span> </td>

<td> <a href="#" class="btn btn-danger remove">- </a> </td>
</tr>
</tbody>
<tfoot>


<tr>
<th>
</th>
<th> Total</th>
<th><span id="sumx">0</span> </th>
</tr>

</tfoot>


<script>
function add() {

var ven1 = document.getElementById('sum1').innerHTML;
var valuey = document.getElementById('result1').innerHTML || 0;
console.log(ven1);
console.log(valuey);
document.getElementById('sumx').innerHTML = parseInt(ven1) + parseInt(valuey);

}
</script>




</table>

</button>

</form>

下面是生成的输入字段JAVASCRIPT

<script type="text/javascript">

$(document).ready(function(){
var postURL = "http://localhost/bcwater/public/addmore";
var i=1;

$('.addRow').on('click', function(){
i++;
addRow();
});

function addRow(){
var tr = '<tr class="dynamic-added">'+
'<td>'+
'</td>'+
'<td><input type="text" name="target[]" id="target2" class="form-control" oninput="calc()" /> </td>'+
'<td><span id="result1">0</span> </td>'+
'<td> <a href="#" class="btn btn-danger remove">- </a> </td>'+

'</tr>';
$('tbody').append(tr);
};

$('tbody').on('click','.remove', function(){
$(this).parent().parent().remove();
});



});

这是 Codei 尝试获取的值,将其乘以 26 并显示 sum1 的答案

    <script>


function calc(){
var value1 = document.getElementById('target').value || 0;
document.getElementById('sum1').innerHTML = parseInt(value1 * 26);

var value2 = document.getElementById('target2').value;
document.getElementById('result1').innerHTML = parseInt(value2 * 26);


var ven1 = document.getElementById('sum1').innerHTML;
var valuey = document.getElementById('result1').innerHTML || 0;
console.log(ven1);
console.log(valuey);
document.getElementById("products").disabled = false;
document.getElementById('sumx').innerHTML = parseInt(ven1) + parseInt(valuey);

}

</script>

请帮忙!!!...我做错了什么

最佳答案

每次调用 addRow() 时,代码中的 ID 始终为 target2。要自动更改 ID,请使用一个变量来保存当前 ID 并连接字符串,您可以使用 var i

$(document).ready(function(){      
var postURL = "http://localhost/bcwater/public/addmore";
var i=1;

$('.addRow').on('click', function(){
i++;
addRow(i);
});

function addRow(i){
var tr = '<tr class="dynamic-added">'+
'<td>'+
'</td>'+
'<td><input type="text" name="target[]" id="target' + i + '" class="form-control" oninput="calc()" /> </td>'+
'<td><span id="result' + i + '">0</span> </td>'+ //This is modified to attach the current index to result
'<td> <a href="#" class="btn btn-danger remove">- </a> </td>'+

'</tr>';
$('tbody').append(tr);
};

$('tbody').on('click','.remove', function(){
$(this).parent().parent().remove();
});
});

i 变量应始终保存添加的最后一行的索引

要相乘,您必须使用相同的 i 变量,函数 calc() 应该如下所示,我建议您重命名 i 为描述性内容:

function calc(){       
var value1 = document.getElementById('target').value || 0;
document.getElementById('sum1').innerHTML = parseInt(value1 * 26);

for(var j = 1; j <= i; j++) {
var value2 = document.getElementById('target' + j).value;
document.getElementById('result' + j).innerHTML = parseInt(value2 * 26); // This is to target the result span that corresponds to the input value
}


var ven1 = document.getElementById('sum1').innerHTML;
var valuey = document.getElementById('result1').innerHTML || 0;
console.log(ven1);
console.log(valuey);
document.getElementById("products").disabled = false;
document.getElementById('sumx').innerHTML = parseInt(ven1) + parseInt(valuey);

}

关于javascript - 如何乘或加动态 javascript 输入字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58266449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com