1

这是我正在尝试为表单中的复选框设置 cookie 的功能

function setCookie(c_name,value,exdays){    
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ?
        "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

function set_check(){
    var proceed = document.forms['form1']['proceed_opt[]'].checked;
    setCookie(proceed_opt[], proceed, 60*60*1);
    alert(proceed_opt[]);
}

我表单中的复选框是这样的..

        <input type="checkbox" name="proceed_opt[]" value="I will upload new Image" onChange="set_check()";>
          Insert Option &quot;I will upload new Image&quot;
    </td>
</tr>
<tr>
    <td></td>
    <td colspan="2">
            &nbsp; 
          <input type="checkbox" name="proceed_opt[]" value="I approve this sample for a Night Light" onChange="set_check()";>
          Insert Option &quot;<font face="Arial, Helvetica, sans-serif" size="2">I approve this sample for a Night Light</font>&quot; 
    </td>
</tr>
<tr>
    <td></td>
    <td colspan="2">
            &nbsp; 
          <input type="checkbox" name="proceed_opt[]" value="Select One" onChange="set_check()";>
          Insert Option &quot;<font face="Arial, Helvetica, sans-serif" size="2">Select One</font>&quot; 
    </td>
</tr>     
<tr>
    <td></td>
    <td colspan='2'>
         &nbsp;
        <input type="checkbox" name="proceed_opt[]" value="other" onChange="set_check()">
          Insert Option &quot;Other See comments&quot;
    </td>
</tr>

但是这里没有设置cookies...谁能告诉我如何为表单中的复选框设置cookies...

4

2 回答 2

1

您的代码存在一些问题。我稍微清理了一下,并做了一些假设。基本上,你把事情复杂化了。

您错误地引用了这些字段,并且出现了错误。这种方法更简单(虽然仍然不是理想的 - 但它有效)。

小提琴:http: //jsfiddle.net/mAnUQ/

HTML:

<form name="form1">
<div>
<input type="checkbox" id="check1" name="proceed_opt[]" value="I will upload new Image" onChange="set_check(this)">
        <label for="check1">I will upload new Image</label>
    </div>
<div>
          <input type="checkbox" id="check2" name="proceed_opt[]" value="I approve this sample for a Night Light" onChange="set_check(this)">
    <label for="check2">I approve this sample for a Night Light</label>
    </div>
<div>              
          <input type="checkbox" id="check3" name="proceed_opt[]" value="Select One" onChange="set_check(this)">
    <label for="check3">Select One</label>
    </div>
    <div>
        <input type="checkbox" id="check4" name="proceed_opt[]" value="other" onChange="set_check(this)">
        <label for="check4">Other See comments</label>
    </div>
</form>

JavaScript:

function setCookie(c_name,value,exdays){    
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ?
        "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

function set_check(me){
    setCookie(me.value, me.checked, 60*60*1);
    console.log(me.value);
    console.log(me.checked);
    console.log(document.cookie)
}
于 2012-05-18T03:02:11.597 回答
0

看看这是否会引导您朝着正确的方向前进。这会设置一个名为“proceed_opt”的 cookie,其中包含复选框的值。

HTML:

<form name="form1"><table><tr><td></td><td>&nbsp; <input type="checkbox" name="proceed_opt[]" value="I will upload new Image" onclick="if(this.checked){set_check(this);}">
      Insert Option &quot;I will upload new Image&quot;</td></tr>
<tr><td></td><td colspan="2">
        &nbsp; 
      <input type="checkbox" name="proceed_opt[]" value="I approve this sample for a Night Light" onclick="if(this.checked){set_check(this);}">
      Insert Option &quot;<font face="Arial, Helvetica, sans-serif" size="2">I approve this sample for a Night Light</font>&quot; </td></tr><tr><td></td><td colspan="2">
        &nbsp; 
      <input type="checkbox" name="proceed_opt[]" value="Select One" onclick="if(this.checked){set_check(this);}">
      Insert Option &quot;<font face="Arial, Helvetica, sans-serif" size="2">Select One</font>&quot; </td></tr>     
 <tr><td></td><td colspan='2'> &nbsp;
 <input type="checkbox" name="proceed_opt[]" value="other" onclick="if(this.checked){set_check(this);}">
     Insert Option &quot;Other See comments&quot;</td></tr>?</table></form>

JAVASCRIPT:

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function set_check(proceed){

var proceed_opt = proceed.value;
setCookie("proceed_opt", proceed_opt, 60*60*1);
alert (proceed_opt);
}
于 2012-05-18T02:25:57.267 回答