[英]Create cookie interface with input fields? (multiple values) and from any domain?
For testing purposes, I want to be able to create cookies as I need them. 为了进行测试,我希望能够根据需要创建cookie。
The cookies I'm using are in this format: A0A60B1381:ASD887DSFFSDF:SD8F89SD7F89SD7 with any generic name. 我使用的Cookie的格式为:A0A60B1381:ASD887DSFFSDF:SD8F89SD7F89SD7,具有任何通用名称。
I need to read the current values of the cookie if it exists(or create a cookie with that name), split the values into separate input fields, be able to edit those input fields, and write the cookie. 我需要读取cookie的当前值(如果存在)(或使用该名称创建一个cookie),将这些值拆分为单独的输入字段,能够编辑这些输入字段并编写cookie。 (and probably destroy the current value of the cookie, if need be).
(如果需要,可能会破坏cookie的当前值)。
What would be the best way to approach this? 解决此问题的最佳方法是什么?
UPDATE: 更新:
Thanks to Abdullah, I have this code now - however, it isn't pulling in cookies from other domains. 多亏了Abdullah,我现在有了这段代码-但是,它没有从其他域中提取Cookie。 So I need it to pull in the cookie - "junk" from any domain, and write it across domains with a expiration date.
因此,我需要它从任何域中提取cookie-“垃圾邮件”,并将其写入具有过期日期的域中。 How would I do that in this context?
在这种情况下,我该怎么做?
<script>
$(document).ready(function() {
var val = $.cookie('junk');
var arr = val.split(':');
$('#Cookie_input0').val(arr[0]); // set value of input element
$('#Cookie_input1').val(arr[1]); // set value of input element
$('#Cookie_input2').val(arr[2]); // set value of input element
$('#Cookie_input3').val(arr[3]); // set value of input element
$('#Cookie_input4').val(arr[4]); // set value of input element
$('#Cookie_input5').val(arr[5]); // set value of input element
$('#Submit').click(function() {
//recreate arr from input elements
var arr = [];
$('input.cookieJr').each(function() {
arr.push($(this).val());
});
// set cookie
var val = arr.join(':');
$.cookie('junk', val); // write the cookie back out
});
});
</script>
And 和
<form name="cookieValues">
<input id="Cookie_input0" class="cookieJr" type="text" />
<input id="Cookie_input1" class="cookieJr" type="text" />
<input id="Cookie_input2" class="cookieJr" type="text" />
<input id="Cookie_input3" class="cookieJr" type="text" />
<input id="Cookie_input4" class="cookieJr" type="text" />
<input id="Cookie_input5" class="cookieJr" type="text" />
<input type="submit" value="Submit" id="Submit"/>
</form>
Use jquery.cookie.js : 使用jquery.cookie.js :
$.cookie('the_cookie'); // gets the cookie
$.cookie('the_cookie', 'A0A60B1381:ASD887DSFFSDF:SD8F89SD7F89SD7'); // sets the cookie
For example: 例如:
var val = $.cookie('the_cookie') || 'A0A60B1381:ASD887DSFFSDF:SD8F89SD7F89SD7';
var arr = val.split(':');
arr[0] = "NEW_VAL";
val = arr.join(':');
$.cookie('the_cookie', val); // write the cookie back out
Update 更新资料
I think you're trying to edit the cookie on the fly, this should work: 我认为您正在尝试即时编辑Cookie,这应该可以:
// after reading cookie as described above, should be inside a $(document).ready()
$('#ID_OF_INPUT_ELEMENT').val(arr[0]); // set value of input element
// once input element has been set and some 'submit' button clicked
$('#ID_OF_SUBMIT_BUTTON').click(function() {
//recreate arr from input elements
var arr = [];
$('input.some_class_they_all_share').each(function() {
arr.push($(this).val());
});
// set cookie
var val = arr.join(':');
$.cookie('the_cookie', val); // write the cookie back out
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.