jQuery ui-tabs .bind("tabsactivate", function (event, ui) 重载时不起作用
jQuery ui-tabs .bind("tabsactivate", function (event, ui) does not work when reloaded
我正在动态创建一个 table(单击按钮)并在激活(单击)选项卡时存储当前选项卡索引。这在我第一次创建 table 时有效;但是,如果我重新创建选项卡以加载不同的数据(隐藏此表单,返回到上一个表单并单击按钮以显示),则以下内容不起作用:
$('#dayTabs').tabs();
$('#dayTabs').bind("tabsactivate", function (event, ui) {
alert('oldTab = ' + ui.oldTab.index());
alert('newTab = ' + ui.newTab.index());
sessionStorage.setItem('ssCurrentTab', ui.newTab.index());
});
但是,以下仍然有效:
$("#menuTabs a").click(function(e){
完整的上下文是:
$('#updateDailyProgram').click(function() {
sessionStorage.setItem('ssFromDate', $("#updateFromDate").val());
$("#menuDetails").empty();
sessionStorage.setItem('ssCurrentDate', $("#updateFromDate").val());
sessionStorage.setItem('ssCurrentTab', 0);
$("#menuDetails").append("<ul id='menuTabs' class='nav nav-tabs'></ul>").find('ul');
var newDateString = $("#updateFromDate").val();
var firstTab = "Y";
for (var i = 0; i < sessionStorage.getItem('ssProgramDays'); i++){
if (firstTab === "Y"){
$("#menuTabs").append("<li class='nav-item'><a href='#home' class='nav-link active' name='"+newDateString+"'>"+newDateString+"</a></li>");
firstTab = "N";
}else{
$("#menuTabs").append("<li class='nav-item'><a href='#home' class='nav-link' name='"+newDateString+"'>"+newDateString+"</a></li>");
}
//Add 1 day to date
newDateString = moment(newDateString, 'DD/MM/YYYY').add(1, 'days');
newDateString = newDateString.format('DD/MM/YYYY');
};
$('#showProgramUpdateForm').hide();
$('#programEditForm').show();
//Show first tab
loadProgramDailyTable($("#updateFromDate").val());
$("#menuTabs a:eq(0)").tab('show');
$('#dayTabs').tabs();
$('#dayTabs').bind("tabsactivate", function (event, ui) {
alert('oldTab = ' + ui.oldTab.index());
alert('newTab = ' + ui.newTab.index());
sessionStorage.setItem('ssCurrentTab', ui.newTab.index());
});
$("#menuTabs a").click(function(e){
e.preventDefault();
sessionStorage.setItem('ssCurrentDate', $(this).attr('name'));
loadProgramDailyTable($(this).attr('name'));
$(this).tab('show');
});
});
HTML:
<div id="dayTabs" class="container">
<span id="menuDetails">
</span>
</div>
我意识到我将 bootstrap 选项卡与 jQuery ui 选项卡混合在一起。解决方案为:
删除:
$('#dayTabs').tabs();
$('#dayTabs').bind("tabsactivate", function (event, ui) {
alert('oldTab = ' + ui.oldTab.index());
alert('newTab = ' + ui.newTab.index());
sessionStorage.setItem('ssCurrentTab', ui.newTab.index());
});
并添加:
sessionStorage.setItem('ssCurrentTab', $(e.target).closest('li').index());
根据:
$("#menuTabs a").click(function(e){
e.preventDefault();
sessionStorage.setItem('ssCurrentTab', $(e.target).closest('li').index());
sessionStorage.setItem('ssCurrentDate', $(this).attr('name'));
loadProgramDailyTable($(this).attr('name'));
$(this).tab('show');
});
我正在动态创建一个 table(单击按钮)并在激活(单击)选项卡时存储当前选项卡索引。这在我第一次创建 table 时有效;但是,如果我重新创建选项卡以加载不同的数据(隐藏此表单,返回到上一个表单并单击按钮以显示),则以下内容不起作用:
$('#dayTabs').tabs();
$('#dayTabs').bind("tabsactivate", function (event, ui) {
alert('oldTab = ' + ui.oldTab.index());
alert('newTab = ' + ui.newTab.index());
sessionStorage.setItem('ssCurrentTab', ui.newTab.index());
});
但是,以下仍然有效:
$("#menuTabs a").click(function(e){
完整的上下文是:
$('#updateDailyProgram').click(function() {
sessionStorage.setItem('ssFromDate', $("#updateFromDate").val());
$("#menuDetails").empty();
sessionStorage.setItem('ssCurrentDate', $("#updateFromDate").val());
sessionStorage.setItem('ssCurrentTab', 0);
$("#menuDetails").append("<ul id='menuTabs' class='nav nav-tabs'></ul>").find('ul');
var newDateString = $("#updateFromDate").val();
var firstTab = "Y";
for (var i = 0; i < sessionStorage.getItem('ssProgramDays'); i++){
if (firstTab === "Y"){
$("#menuTabs").append("<li class='nav-item'><a href='#home' class='nav-link active' name='"+newDateString+"'>"+newDateString+"</a></li>");
firstTab = "N";
}else{
$("#menuTabs").append("<li class='nav-item'><a href='#home' class='nav-link' name='"+newDateString+"'>"+newDateString+"</a></li>");
}
//Add 1 day to date
newDateString = moment(newDateString, 'DD/MM/YYYY').add(1, 'days');
newDateString = newDateString.format('DD/MM/YYYY');
};
$('#showProgramUpdateForm').hide();
$('#programEditForm').show();
//Show first tab
loadProgramDailyTable($("#updateFromDate").val());
$("#menuTabs a:eq(0)").tab('show');
$('#dayTabs').tabs();
$('#dayTabs').bind("tabsactivate", function (event, ui) {
alert('oldTab = ' + ui.oldTab.index());
alert('newTab = ' + ui.newTab.index());
sessionStorage.setItem('ssCurrentTab', ui.newTab.index());
});
$("#menuTabs a").click(function(e){
e.preventDefault();
sessionStorage.setItem('ssCurrentDate', $(this).attr('name'));
loadProgramDailyTable($(this).attr('name'));
$(this).tab('show');
});
});
HTML:
<div id="dayTabs" class="container">
<span id="menuDetails">
</span>
</div>
我意识到我将 bootstrap 选项卡与 jQuery ui 选项卡混合在一起。解决方案为:
删除:
$('#dayTabs').tabs();
$('#dayTabs').bind("tabsactivate", function (event, ui) {
alert('oldTab = ' + ui.oldTab.index());
alert('newTab = ' + ui.newTab.index());
sessionStorage.setItem('ssCurrentTab', ui.newTab.index());
});
并添加:
sessionStorage.setItem('ssCurrentTab', $(e.target).closest('li').index());
根据:
$("#menuTabs a").click(function(e){
e.preventDefault();
sessionStorage.setItem('ssCurrentTab', $(e.target).closest('li').index());
sessionStorage.setItem('ssCurrentDate', $(this).attr('name'));
loadProgramDailyTable($(this).attr('name'));
$(this).tab('show');
});