0
我有以下文檔(列出如下)將「金額1」乘以2(am1 * 2),並將「金額2」中的值更改爲結果(由JavaScript執行)。將表單域(... name =「array1 []」)傳遞給Javascript函數並更改其他表單域的值(... name =「array2 []」)
然後,相同的表單具有向表單添加字段(金額1 &金額2)的功能。
我希望在其他「ADDED」字段中具有相同的功能(次數「金額1」,並更改「金額2」的值)。
<html>
<head>
<script>
var ct = 1;
function new_combi(){
ct++;
var div1 = document.createElement('div');
div1.id = ct;
// link to delete extended form elements
var delLink = '<div style="text-align:right;margin-right:65px"><a href="javascript:delIt('+ ct +')">Del</a></div>';
div1.innerHTML = document.getElementById('amount').innerHTML + delLink;
document.getElementById('combine').appendChild(div1);
}
// function to delete the newly added set of elements
function delIt(eleId){
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('combine');
parentEle.removeChild(ele);
}
function calculate(){
am1 = document.form1["am1[]"].value;
total = am1*2;
document.form1["am2[]"].value = total;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<div id="combine">
<div>
<table>
<tr>
<td width="25%" valign="top">Amount 1</td>
<td valign="top"><input type="text" name="am1[]" onchange="calculate()"/></td>
</tr>
<tr>
<td valign="top">Amount 2</td>
<td valign="top"><input type="text" name="am2[]" /></td>
</tr>
</table>
</div>
</div>
<p id="addnew">
<a href="javascript:new_combi()">Add Combination</a>
</p>
</form>
<!-- Template to add-->
<div id="amount" style="display:none">
<div>
<table>
<tr>
<td width="25%" valign="top">Amount 1</td>
<td valign="top"><input type="text" name="am1[]" onchange="calculate()"/></td>
</tr>
<tr>
<td valign="top">Amount 2</td>
<td valign="top"><input type="text" name="am2[]" /></td>
</tr>
</table>
</div>
</div>
</body>
</html>
輝煌,工作完美!非常感謝你! :D – Bernedette
更改:)如果更改了輸入[1](手動計算後),並且添加了其他組合,則「更改的值」將恢復爲計算(更改的數據將被重置)。有沒有一種方法可以保持更改的值? – Bernedette