将表单数据设置为 javascript 中的 cookie 值
Setting form data to a cookie value in javascript
我正在尝试将表单数据设置为 cookie 值
野兔是我的代码:
html:
<form method="POST" action="#" id="address-form">
<select name="city">
<option value="a city">A City</option>
</select>
<select name="pincode" id="pincodes">
<option value="">Select Pincode</option>
<option value="pin1">pin1</option>
<option value="pin2">pin2</option>
</select>
<br><br>
<input type="submit">
</form>
JS:
//set the cookies
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// get the cookies
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// check the cookies
function checkCookie() {
var user = getCookie("username");
// function mmm() {
// $("form").submit(function (e) {
// e.preventDefault();
// user = $('#pincodes').val();
// });
// return user;
// }
if (user !== "") {
console.log("Pincode is: " + user);
} else {
openModal();
user = $("#pincodes option:selected").val();
console.log("Pincode not set");
if (user !== "" && user !== null) {
setCookie("username", user, 365);
}
}
}
//execute the cookie function
checkCookie();
但是当我刷新页面时,它说 "Pincode not set" 并且每次都打开模式并且没有设置 cookie。
你可以检查这个fiddle:jsfiddle.net/bharatsing/wga5t2jw/
$( "#address-form" ).submit(function( event ) {
var user = $("#pincodes option:selected").val();
if (user !== "" && user !== null) {
setCookie("username", user, 365);
}
event.preventDefault();
});
我正在尝试将表单数据设置为 cookie 值
野兔是我的代码:
html:
<form method="POST" action="#" id="address-form">
<select name="city">
<option value="a city">A City</option>
</select>
<select name="pincode" id="pincodes">
<option value="">Select Pincode</option>
<option value="pin1">pin1</option>
<option value="pin2">pin2</option>
</select>
<br><br>
<input type="submit">
</form>
JS:
//set the cookies
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// get the cookies
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// check the cookies
function checkCookie() {
var user = getCookie("username");
// function mmm() {
// $("form").submit(function (e) {
// e.preventDefault();
// user = $('#pincodes').val();
// });
// return user;
// }
if (user !== "") {
console.log("Pincode is: " + user);
} else {
openModal();
user = $("#pincodes option:selected").val();
console.log("Pincode not set");
if (user !== "" && user !== null) {
setCookie("username", user, 365);
}
}
}
//execute the cookie function
checkCookie();
但是当我刷新页面时,它说 "Pincode not set" 并且每次都打开模式并且没有设置 cookie。
你可以检查这个fiddle:jsfiddle.net/bharatsing/wga5t2jw/
$( "#address-form" ).submit(function( event ) {
var user = $("#pincodes option:selected").val();
if (user !== "" && user !== null) {
setCookie("username", user, 365);
}
event.preventDefault();
});