gpt4 book ai didi

javascript - 在表单之间传递信息 Javascript

转载 作者:行者123 更新时间:2023-11-28 10:49:16 26 4
gpt4 key购买 nike

我有一个非常基本的表单,我想在不使用 php 的情况下从另一个表单传递数据。两种表单都正确显示,但我无法弄清楚如何使用 JavaScript 代码将数据从第一个表单传输到第二个表单。

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<title>HTML Form</title>
</head>

<body>
<form action"" method="get" onsubmit="submitForm();" name="submitForm">

Company Name:<input type="text"name="companyName"><br><br>
Owner: <input type="text" name="compOwner"><br><br>
Address: <input type="text" name="address"><br><br>
Phone Number:<input type="text" name="phoneNum"><br><br>
Annual Sales($)<input type="text" name="annualSales"><br><br>
Borrow Amount($)<input type="text" name="borrowAmount"><br><br>
Payment Terms(Months)<input type="text" name="paymentTerms"><br><br>

<input type="submit" value="Submit" onclick="submitForm();">
</form>

<form action"" method="get" name="secondForm">
Name of Borrowing Company<input type="text"id="compName"><br><br>
Annual Sales<input type="text"id="annSales"><br><br>
Borrow Amount<input type="text"id="amountBorrowed"><br><br>
Payment Terms<input type="text"id="payTerms"><br><br>

Total Interest On Loan<input type="text"id="totalInterest"><br><br>
Total Payment<input type="text"id="totalPay"><br><br>
Total Profit <input type="text"enabled="false" id="totalProfit"><br><br>




</form>
<script>
function submitForm()
{
compName = document.forms["submitForm"]["companyName"].value;
annualSales = document.forms["submitForm"]["annualSales"].value;
borrowAmount = document.forms["submitForm"]["borrowAmount"].value;
months = document.forms["submitForm"]["paymentTerms"].value;
totalInterest = borrowAmount * (months/12) * 0.03;
totalPayment = borrowAmount + totalInterest;
profit = totalPayment - borrowAmount;


document.getElementById('compName').setAttribute(value, compName);
document.getElementById('annSales').value = annualSales;
document.getElementById('amountBorrowed').value = borrowAmount;
document.getElementById('payTerms').value = months;
document.getElementById('totalInterest').value = totalInterest;
document.getElementById('totalPay').value = totalPayment;
document.getElementById('totalProfit').value = profit;

}
</script>


</body>
</html>

最佳答案

我明白了。这段代码实际上有几个问题。

首先,第一个表单元素的名称与函数的名称冲突。这是因为当一个元素有 name 时属性,它在 JS 全局范围内创建一个新变量并重写您的函数,因为它具有相同的名称。您的表单名称属性必须是您的函数名称的其他内容。

<form action"" method="get" onsubmit="submitForm();" name="submitF">

下一步,input type="submit"刷新页面,这可能是您不希望发生的。您可以在表单后面添加一个按钮元素。

</form>
<button onclick="submitForm()">Submit</button>

在 JS 中,submitF 是表单元素中的一个变量(感谢 name 属性),因此您可以在不使用 document.forms 的情况下写得更简单。 .

function submitForm() {
compName = submitF["companyName"].value;
annualSales = submitF["annualSales"].value;
borrowAmount = submitF["borrowAmount"].value;
months = submitF["paymentTerms"].value;
(...)
}

此外,setAttribute(value, compName)不起作用。事实并非如此。分配给value相反,就像代码的其余部分一样。

我的整个工作代码如下所示:

<html>
<head>
<title>HTML Form</title>
<script type="text/javascript">
function submitForm() {
compName = submitF["companyName"].value;
annualSales = submitF["annualSales"].value;
borrowAmount = submitF["borrowAmount"].value;
months = submitF["paymentTerms"].value;
totalInterest = borrowAmount * (months/12) * 0.03;
totalPayment = borrowAmount + totalInterest;
profit = totalPayment - borrowAmount;


document.getElementById('compName').value = compName;
document.getElementById('annSales').value = annualSales;
document.getElementById('amountBorrowed').value = borrowAmount;
document.getElementById('payTerms').value = months;
document.getElementById('totalInterest').value = totalInterest;
document.getElementById('totalPay').value = totalPayment;
document.getElementById('totalProfit').value = profit;
}

</script>
</head>

<body>
<form action"" method="get" onsubmit="submitForm();" name="submitF">

Company Name:<input type="text"name="companyName"><br><br>
Owner: <input type="text" name="compOwner"><br><br>
Address: <input type="text" name="address"><br><br>
Phone Number:<input type="text" name="phoneNum"><br><br>
Annual Sales($)<input type="text" name="annualSales"><br><br>
Borrow Amount($)<input type="text" name="borrowAmount"><br><br>
Payment Terms(Months)<input type="text" name="paymentTerms"><br><br>


</form>
<button onclick="submitForm()">Submit</button>
<form action"" method="get" name="secondForm">
Name of Borrowing Company<input type="text"id="compName"><br><br>
Annual Sales<input type="text"id="annSales"><br><br>
Borrow Amount<input type="text"id="amountBorrowed"><br><br>
Payment Terms<input type="text"id="payTerms"><br><br>

Total Interest On Loan<input type="text"id="totalInterest"><br><br>
Total Payment<input type="text"id="totalPay"><br><br>
Total Profit <input type="text"enabled="false" id="totalProfit"><br><br>
</form>
</body>
</html>

就是这样。希望我有所帮助。

编辑:此外,您应该始终将 JS 代码放在 <head> 中,除非你有充分的理由不这样做。

关于javascript - 在表单之间传递信息 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340821/

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