【问题标题】:Javascript add cookieJavascript 添加 cookie
【发布时间】:2015-03-11 07:50:26
【问题描述】:

我对 javascript 还很陌生,不太确定自己在做什么。现在我正在尝试将文本框数据以及一些字符串添加到 cookie 以显示在另一个页面上。文本框部分正在工作。

对于字符串,我正在获取单选按钮的值(用于多项选择测验验证),如果该值是该组的正确值,我想向 cookie 添加一个字符串。我无法弄清楚我在这部分做错了什么。

具体不工作的是gradeit()函数中的内容

JS 文件

var total = 5;
var right = 0;

//cookies
function addToCookie(id, value)
{
  document.cookie = id  + escape(value);

}

var grade=new Array()

function gradeit(){
if(document.getElementById('correctOne').checked)
    {
      right++;
      addToCookie("Q1 - Correct",right);
    }
    else {addToCOokie("Q1 - Incorrect", right);}
}


function checkCookie() {
var firstName = document.getElementById("fname").value;
var lastName = document.getElementById("lname").value;
var email =  document.getElementById("email").value;

    addToCookie("First Name= ",firstName);
    addToCookie("Last Name= ",lastName);
    addToCookie("Email=",email);
 }

window.onload = function () {
var elem = document.getElementById("submit");
elem.addEventListener('click', checkCookie);
}

// determine whether there is a cookie
var allcookies = document.cookie;
alert("All Cookies : " + allcookies);
// Get all the cookies pairs in an array
cookiearray = allcookies.split(';');

var result = "";

// Now take key value pair out of this array
for (var i = 0; i < cookiearray.length; i++) {
  name = cookiearray[i].split('=')[0];
value = cookiearray[i].split('=')[1];
result +=( name + " is : " + value)+"<br>";


 }
document.writeln(result);

HTML 页面

 <DOCTYPE HTML5>
 <html>
 <head>
   <meta charset="utf-8">
   <title>Quiz</title>
   <script src="cookies.js" type="text/javascript"></script>
 </head>
 <body>
   <h1><b>Multiple Choice Quiz</b></h1>
   <form name="myquiz" action="answers.html" method="post">

  <h2>Please Enter the Following:</h2>
    First Name:  <input type="text" id="fname"></input>
    Last Name:   <input type="text" id="lname"></input><br><br> 
    Student Email:   <input type="email" id="email"></input>
<br>
  <h3>#1. What is the capital of Iowa?</h3>
    <input type="radio" name="question1" id="correctOne">Des Moines</input>
    <input type="radio" name="question1" id="wrong">Los Angeles</input>
    <input type="radio" name="question1" id="wrong">Paris</input>
    <input type="radio" name="question1" id="wrong">Tokyo</input>

 <br><br>
     <input type="submit" id="submit" value="Submit Answers" onClick="gradeit()"></input>
 </form>
 </body>
 </html>

【问题讨论】:

  • checkCookie 如何检查 cookie?
  • 通过底部的事件监听器,var elem 是我的提交按钮
  • 仅仅因为你有一个click 事件并不意味着它真的检查了一个cookie。
  • 对,但这也是目前正在工作的部分。点击调用检查cookie,然后将addToCookie参数发送到addToCookie函数(至少我认为)。

标签: javascript html string post cookies


【解决方案1】:

我刚刚通过这样修改我的gradeit() 函数让它工作:

function gradeit(){
var ans = "";
if(document.getElementById('correctOne').checked)
    {
      //document.getElementById("output").innerHTML = "Q1 - Correct";
      right++;
      ans ="Right";
      addToCookie("Q1 - ", ans);
    }
    else {
        ans ="Wrong";
        addToCookie("Q1 - ", ans);
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2016-09-20
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多