我已经尝试解决这个问题大约一周了,但没有成功。我试图将表单字段中的值保存到 cookie onclick="updatePricingFunction() 但当我单击附加到 onclick 的 updatePricingFunction 按钮时,该值不会更改为新数字。任何人都可以帮助我吗?
function updatePricingFunction(){
var beforeNoonField = document.getElementById("beforeNoonNPSlot"); // beforeNoonNPSlot is a form id.
document.cookie = "beforeNoonCookie=" + beforeNoonField; // Create the cookie
}
最佳答案
哈哈,你的代码有点错误,但是为了争论,你有
<form name="myform">
Enter Number<input type="text" name="sampletext" id="beforeNoonNPSlot">
<button type="button" onclick="updatePricingFunction()">Set Cookie</button>
</form>
您需要获取表单的名称以及要从中获取值的文本框的名称。这样,当单击按钮时,将调用函数 updatePricingFunction(),它将从表单中的文本框中检索值
function updatePricingFunction()
{
//To get a value in a text field, you must have your variable equals
//document.forms["(form name here)"]["(name of the text field here)"].value
var beforeNoonField = document.forms["myform"]["sampletext"].value;//This gets your value
document.cookie = "beforeNoonCookie=" + beforeNoonField; // Create the cookie
}
请注意,此方法是使用 javascript 获取值的更传统方法,因此您应该更频繁地使用此方法。
关于javascript - 将表单数据保存到 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24874776/