javascript - 将表单数据保存到 cookie

标签 javascript cookies

我已经尝试解决这个问题大约一周了,但没有成功。我试图将表单字段中的值保存到 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/

相关文章:

javascript - 如何使用crossfilter和DC.js处理缺失数据?

javascript - getElementById 检查多个条件

php - 使用 php 如何记住文本字段并使用 cookie 在下次访问该网站时显示它?

java - 禁用 Cookies 也会停止 Session 吗?

javascript - 获取正确的 URL 以从 JS 文件进行 ajax 调用

javascript - 用于 crashlytics 的 Firebase 云功能未触发

javascript - 从 jquery 方法中提取变量

javascript - 如何在选项卡关闭时删除 jquery cookie

android - 如何在 Flutter 上使用 cookie 发出 http 请求?

javascript - $rootScope 未定义