我正在尝试为Cookie添加新值,具体取决于您在下拉菜单中选择的选项。选择选项时,应将该选项的值插入cookie中。当您选择其他选项时,应将该选项的新值添加到cookie中,并应替换旧值。
我试过这个:
var value;
var id;
var service_info;
var service;
var connection; //Namnet på de tjänster som finns, nao_lan, stadsnat, tele osv English: The values of the options
$.cookie("cookie_connection", connection); //Creates cookie
$('.type_of_connection').change(function(e) { //Visa vald tjänst och dess tillhörande div. Varje tjänst har en egen div.
var show_connection = 1;
connection = $(this).val(); //Innehåller VILKEN tjänst man har valt ENGLIS: The option you choosed
$.cookie("cookie_connection", connection);
var divbox = null; //Innehåller id:et på den div som skall visas.
if(connection == "nao_lan") {
divbox = "services_naolan";
} else if(connection == "stadsnat") {
divbox = "services_stadsnat";
} else if(connection == "vadsl") {
divbox = "services_vadsl";
} else if(connection == "tele") {
divbox = "services_tele";
} else {
divbox = null;
}
$.ajax({
type: 'GET',
url: 'summary.php',
data: {connection: connection, show_connection: show_connection},
dataType: "html",
success: function(data) { $("#table_right #" + divbox).html(data).show(); },
error: function(xhr, ajaxOptions, thrownError) { alert(thrownError); }
});
});
$('#'+$.cookie('cookie_connection')+' :input').blur(function(e) {
alert($.cookie('cookie_connection'));
if(!$(e.currentTarget).val() == "") {
service_info = $(e.currentTarget).val(); //Innehåller värdet i input-fälten
id = $(e.currentTarget).attr('id');
service = $.cookie('cookie_connection');
$.ajax({
type: 'GET',
url: 'summary.php',
data: {service_info: service_info, id: id, service: service},
dataType: "html",
success: function(data) { $('#table_right #services_'+$.cookie('cookie_connection')).html(data); },
error: function(xhr, ajaxOptions, thrownError) { alert(thrownError); }
});
}
});
如您所见,即时尝试在第一个函数中为cookie添加新值。我将在稍后的函数中使用此cookie值来生成属于cookie值的div。谁可以帮助我?
答案 0 :(得分:0)
我一直使用这两个功能来设置和获取cookie:
function setCookie(c_name,value,exdays,c_domain) {
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 + "; path=/; domain=."+c_domain;
}
function getCookie(c_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1) {
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1) {
c_value = "";
} else {
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1) {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
示例:
setCookie("coolcookie","SOME VALUE",7,"stackoverflow.com");
var mycookie = getCookie("coolcookie");
答案 1 :(得分:0)
我认为你必须在更改事件中绑定模糊事件,否则它将无法工作 - 即你必须解除旧的模糊事件的绑定,并创建一个新事件,因为你的连接东西已经改变了。我认为以下代码可能有所帮助:
function bindConnection( prevConnId, currConnId ) {
if( prevConnId ) {
$('#'+prevConnId+' :input').off( "blur ");
}
$('#'+currConnId+' :input').on( "blur", function(e) {
alert( currConnId );
if(!$(e.currentTarget).val() == "") {
service_info = $(e.currentTarget).val(); //Innehåller värdet i input-fälten
id = $(e.currentTarget).attr('id');
service = currConnId;
$.ajax({
type: 'GET',
url: 'summary.php',
data: {service_info: service_info, id: id, service: service},
dataType: "html",
success: function(data) { $('#table_right #services_'+currConnId).html(data); },
error: function(xhr, ajaxOptions, thrownError) { alert(thrownError); }
});
}
});
}
$('.type_of_connection').change(function(e) { //Visa vald tjänst och dess tillhörande div. Varje tjänst har en egen div.
var show_connection = 1;
connection = $(this).val(); //Innehåller VILKEN tjänst man har valt ENGLIS: The option you choosed
bindConnection( $.cookie("cookie_connection"), connection ); //unbind event for previous connection, and bind new one
$.cookie("cookie_connection", connection);
var divbox = null; //Innehåller id:et på den div som skall visas.
if(connection == "nao_lan") {
divbox = "services_naolan";
} else if(connection == "stadsnat") {
divbox = "services_stadsnat";
} else if(connection == "vadsl") {
divbox = "services_vadsl";
} else if(connection == "tele") {
divbox = "services_tele";
} else {
divbox = null;
}
$.ajax({
type: 'GET',
url: 'summary.php',
data: {connection: connection, show_connection: show_connection},
dataType: "html",
success: function(data) { $("#table_right #" + divbox).html(data).show(); },
error: function(xhr, ajaxOptions, thrownError) { alert(thrownError); }
});
});
bindConnection( null, $.cookie('cookie_connection') ); //bind the first connection