Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用表单输入设置cookie_Javascript_Forms_Cookies - Fatal编程技术网

Javascript 使用表单输入设置cookie

Javascript 使用表单输入设置cookie,javascript,forms,cookies,Javascript,Forms,Cookies,我正在尝试使用用户对web表单的输入制作用户名cookie。但是它不起作用,我不知道为什么。你知道问题出在哪里吗 <form> <input type="text" value="Enter Your Nickname" id="nameBox"> <input type="button" value="Go!" id="submit" onClick="putCookie"> <form> <script>

我正在尝试使用用户对web表单的输入制作用户名cookie。但是它不起作用,我不知道为什么。你知道问题出在哪里吗

<form>
    <input type="text" value="Enter Your Nickname" id="nameBox">
    <input type="button" value="Go!" id="submit" onClick="putCookie">
<form>


<script>
    var today = new Date();
    var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); // plus 30 days

    function setCookie(name, value){
        document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
    }
    //this should set the UserName cookie to the proper value;
    function storeValues(form){
        setCookie("userName", form.submit.value);
        return true;
    }

</script>
</body>

var today=新日期();
var expiry=新日期(今天.getTime()+30*24*3600*1000);//加30天
函数setCookie(名称、值){
document.cookie=name+“=”+escape(value)+“path=/;expires=“+expire.togmString()”;
}
//这应该将用户名cookie设置为正确的值;
函数存储值(表单){
setCookie(“用户名”,form.submit.value);
返回true;
}
将表单更改为:

<form onsubmit="storeValues(this)">
<input type="text" value="Enter Your Nickname" id="nameBox">
<input type="submit" value="Go!" id="submit">
<form>

您可以查看下面的代码,它可能会对您有所帮助

<html>
<head>
    <script>
var today = new Date();
  var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); // plus 30 days

  function setCookie(name, value)
  {
    document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
  }
function putCookie(form)
                //this should set the UserName cookie to the proper value;
  {
   setCookie("userName", form[0].usrname.value);

    return true;
  }

  </script>
</head>
<body>
<form>
 <input type="text" value="Enter Your Nickname" id="nameBox" name='usrname'>
 <input type="button" value="Go!" id="submit" onclick="putCookie(document.getElementsByTagName('form'));">
</form>
</body>


</html>

var today=新日期();
var expiry=新日期(今天.getTime()+30*24*3600*1000);//加30天
函数setCookie(名称、值)
{
document.cookie=name+“=”+escape(value)+“path=/;expires=“+expire.togmString()”;
}
函数(表格)
//这应该将用户名cookie设置为正确的值;
{
setCookie(“用户名”,形式[0].usrname.value);
返回true;
}
虽然定义的函数名应为putCookies,而不是storeValues和函数调用,但您可以这样做: putCookie(document.getElementsByTagName('form')

在函数定义内部,可以从以下表单获取cookie值: setCookie(“用户名”,形式[0].usrname.value)

表单元素应具有以下属性:name='usrname'


它肯定会使用form元素为您的用户名设置cookie。

javascript代码中没有putCookie函数;看起来您将其命名为setCookiefixed,它在定义的
escape(value)
函数中仍然不工作?返回的内容是什么?为什么您的
脚本
正文
之外,而
头部
html
之内?这是无效的标记。另外,您是否定义了
表单
提交
?我已将脚本放在正文中。可能我没有正确检查Cookie。我刚刚修改了html元素的几个属性,所有其他属性都是您的代码,以在@user4485835处计算出来,如何连接它们?
<html>
<head>
    <script>
var today = new Date();
  var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); // plus 30 days

  function setCookie(name, value)
  {
    document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
  }
function putCookie(form)
                //this should set the UserName cookie to the proper value;
  {
   setCookie("userName", form[0].usrname.value);

    return true;
  }

  </script>
</head>
<body>
<form>
 <input type="text" value="Enter Your Nickname" id="nameBox" name='usrname'>
 <input type="button" value="Go!" id="submit" onclick="putCookie(document.getElementsByTagName('form'));">
</form>
</body>


</html>