Javascript 无法理解为什么我可以';不要按名称读取java Cookie

Javascript 无法理解为什么我可以';不要按名称读取java Cookie,javascript,cookies,Javascript,Cookies,好吧,我已经花了好几个小时来研究这个问题,现在我已经完全秃顶了,我要认输了,看看是否有人能指出(我相信很简单)错误,那就是把我的代码变成无用的垃圾 我有两个页面,第一个页面由用户输入3个输入,并保存为cookie: <script type="text/javascript"> function CC() { var V1Box = document.getElementById("V1"); var V2Box = do

好吧,我已经花了好几个小时来研究这个问题,现在我已经完全秃顶了,我要认输了,看看是否有人能指出(我相信很简单)错误,那就是把我的代码变成无用的垃圾

我有两个页面,第一个页面由用户输入3个输入,并保存为cookie:

    <script type="text/javascript">
     function CC()
      {
        var V1Box = document.getElementById("V1");
        var V2Box = document.getElementById("V2");
        var V3Box = document.getElementById("V3");
        var V1 = V1Box.value;
        var V2 = V2Box.value;
        var V3 = V3Box.value;

           document.cookie = "V1 = " + V1 + "; path=/";
           document.cookie = "V2 = " + V2 + "; path=/";
           document.cookie = "V3 = " + V3 + "; path=/";

        window.location = "pg2.html";
      }

</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="Generator" content="Serif WebPlus X5">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">
<title>db2</title>
<style type="text/css">
body {margin: 0px; padding: 0px;}
.Artistic-Body-C
{
    font-family:"Verdana", sans-serif; font-size:27.0px; line-height:1.19em;
}
</style>
<link rel="stylesheet" href="wpscripts/wpstyles.css" type="text/css">
</head>
<body text="#000000" style="background-color:#ffffff; text-align:center; height:900px;">
<div style="background-color:transparent;text-align:left;margin-left:auto;margin
right:auto;position:relative;width:375px;height:900px;">
<form id="form_1" action="" method="post" target="_self" style="margin:0px;">
<input type="button" style="position:absolute; left:271px; top:12px; width:81px;
height:22px;" value="Submit" onClick="CC()">
<input type="text" id="V1" name="V1" value="" style="position:absolute; left:23px;
top:12px; width:227px;">
<input type="text" id="V2" name="V2" value="" style="position:absolute; left:23px;
top:46px; width:227px;">
<input type="text" id="V3" name="V3" value="" style="position:absolute; left:23px;
top:80px; width:227px;">
</form>
</div>
</body>
</html>

函数CC()
{
var V1Box=document.getElementById(“V1”);
var V2Box=document.getElementById(“V2”);
var V3Box=document.getElementById(“V3”);
var V1=V1.0值;
var V2=V2Box.value;
var V3=V3Box.value;
document.cookie=“V1=”+V1+“路径=/”;
document.cookie=“V2=“+V2+”;路径=/”;
document.cookie=“V3=“+V3+”;路径=/”;
window.location=“pg2.html”;
}
db2
正文{margin:0px;padding:0px;}
.艺术体
{
字体系列:“Verdana”,无衬线;字体大小:27.0px;线条高度:1.19em;
}
第二个页面应获取先前设置的cookie并隔离这些值:

<script type="text/javascript">

window.onload = function getCookie(){

var allcookies = document.cookie;
   cookiearray  = allcookies.split(';');

   for(var i=0; i < cookiearray.length; i++){
      var name = cookiearray[i].split('=')[0];
      var value = cookiearray[i].split('=')[1];
        if(name == 'V1'){var V1 = value;}
        if(name == 'V2'){var V2 = value;}
        if(name == 'V3'){var V3 = value;}
   }

document.getElementById("V1").innerHTML += V2;
document.getElementById("V2").innerHTML += V1;
document.getElementById("V3").innerHTML += V3;
}

</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="Generator" content="Serif WebPlus X5">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">
<title>db2</title>
<style type="text/css">
body {margin: 0px; padding: 0px;}
.Artistic-Body-C
{
    font-family:"Verdana", sans-serif; font-size:27.0px; line-height:1.19em;
}
</style>
<link rel="stylesheet" href="wpscripts/wpstyles.css" type="text/css">
</head>
<body text="#000000" style="background-color:#ffffff; text-align:center; height:900px;">
<div style="background-color:transparent;text-align:left;margin-left:auto;margin
right:auto;position:relative;width:375px;height:900px;">
<div style="position:absolute;left:23px;top:12px;">
    <div class="Wp-Artistic-Body-P">
        <span class="Artistic-Body-C" id="V1">V1: </span></br>
        <span class="Artistic-Body-C" id="V2">V2: </span></br>
        <span class="Artistic-Body-C" id="V3">V3: </span></br></div>
</div>
</div>
</body>
</html>

window.onload=函数getCookie(){
var allcookies=document.cookie;
cookiearray=allcookies.split(“;”);
对于(var i=0;i
V2:
V3:
我遇到的问题是,代码无法识别这些if语句中的任何名称——即使我回显了名称和值;它将回显“V3”和相应的值,但V3变量从未设置,将回显为“未定义”。我在试图弄清楚如何将名称设置为“V3”,但却无法识别“if(name=='V3'){var V3=value)”并保持var V3未定义。我甚至测试了“if(name!='V3'){var V3=value)”,甚至在回显名称并获取“V3”时它将在if语句中解释名称不等于V3,并实际设置适当的V3变量值。我不明白它如何正确命名所有内容,正确检索所有内容,然后不识别if语句中的名称以正确设置变量——不管我调用的是V1、V2还是V3;它只是看不到if语句中的ose名称

我确信这在我的语法中是愚蠢的,或者是有拼写错误之类的,但在我自己尝试修复它三个小时后,我显然对它视而不见。我认识到还有其他更简单的方法来操纵用户输入,但我的项目作为一个整体要求我使用cookies,并且我能够在不同的页面上按名称检索它们;我如果您对我如何更好地完成这一标准有任何建议,我将非常感激,但是,让我当前的代码正常工作将是一个很大的帮助,我的头皮(当我继续抓任何剩余的头发时,它已经开始大量出血)将非常感谢您。


<script type="text/javascript">


function getCookie(){

var allcookies = document.cookie;
   cookiearray  = allcookies.split(';');

   for(var i=0; i < cookiearray.length; i++){
      var name = cookiearray[i].split('=')[0];
      var value = cookiearray[i].split('=')[1];
        if(name == 'V1'){var V1 = value;}
        if(name == 'V2'){var V2 = value;}
        if(name == 'V3'){var V3 = value;}
   }

document.getElementById("V1").innerHTML += V2;
document.getElementById("V2").innerHTML += V1;
document.getElementById("V3").innerHTML += V3;
}

window.onload = getCookie; 
</script>
函数getCookie(){ var allcookies=document.cookie; cookiearray=allcookies.split(“;”); 对于(var i=0;i

我不相信getCookie函数曾经运行过。

看看V1、V2、V3是在哪里声明的

    if(name == 'V1'){var V1 = value;}   <-- what happens if not true
    if(name == 'V2'){var V2 = value;}   <-- what happens if not true
    if(name == 'V3'){var V3 = value;}   <-- what happens if not true
声明并将var放入ifs中

JSLint/JSHint会指出这个错误


第三个问题是innerHTML不能有+=

document.getElementById("V1").innerHTML = document.getElementById("V1").innerHTML + V1;


另一个问题是名称中的空白。Cookie在;后面有一个空格,您在名称中没有说明。

java或javascript?上次我检查它们是两种不同的东西:)JSLInt您的javascript代码并查看错误。谢谢,我从这一切中开始使用JSLInt,所以至少这对我今后的工作有帮助不幸的是,在这种情况下,它给了我一大堆不影响应用程序的小问题,然后在解决我的问题之前就停止了(47%)因此,即使使用了该工具,我还是迷路了。感谢您的快速响应!不幸的是,这似乎仍然无法解决我的问题--现在var V3返回为空而不是未定义。它似乎仍然无法将名称识别为V3(即使如果我回显“name”,它会告诉我是这样)。我想知道您是否介意向我展示您的解决方案是什么样子的“全部”,因为我可能只是定义了不正确的顺序的空值(目前在我开始“for”循环之前就有了它)再次感谢,不幸的是,我仍然没有得到cookie数据。我已经将这段代码拆开,并以一千种不同的方式重新组装了它。我相信这一点,问题直接在于它没有正确解释if语句中的名称。这真的令人困惑和沮丧;如果我叫这个名称,它会给我最后一个在循环中--“V3”,但是它不会识别“if(name=='V3')”一如既往。如果您能提供任何帮助,我们将不胜感激;我不知道我是否能应付另一天的失败。简单的调试会显示错误。请观看警报,您看到了什么?非常感谢您的帮助--说真的,我开始有问题了!我很好奇,如果您可以的话
document.getElementById("V1").innerHTML = document.getElementById("V1").innerHTML + V1;