我正在尝试向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。有人可以帮助我吗?
我认为您必须将blur事件绑定在change事件内部,否则它将永远无法工作-即您必须取消绑定旧的blur事件,并创建一个新事件,因为您的连接内容已更改。我认为以下代码可能会有所帮助:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句