如果您的.Button
类位于加载的内容中,您将需要使用jQuery的live功能。
$('.Button').live('click', function() {
//Here I want to reload the current selected tab
$tabs.tabs('load', $tabs.tabs('option', 'selected'));
return false;
});
而且,由于.Button
是一个链接,你需要添加return false;
该函数内部。
从寻找你的代码,我不知道为什么你把它设置为不能加载标签,直到你点击一个后。同时点击任何标签将始终加载相同的内容(网址不会更改)。最后,你不应该需要使用eval()
脚本(这可能是问题吗?)。
除了这些问题,它看起来像你的代码应该工作。
我也不清楚自己的JSON如何格式化,所以我改写了这个假设以下JSON格式:
({
"items": [
{ "title": "example 1" },
{ "title": "example 2" },
{ "title": "example 3" },
{ "title": "example 4" },
{ "title": "example 5" },
{ "title": "example 6" },
{ "title": "example 7" },
{ "title": "example 8" },
{ "title": "example 9" },
{ "title": "example 10" }
]
})
脚本
$(function() {
var $tabs = $(".Tabs").tabs({
selected: null,
select: function(event, ui) {
loadTab(ui.index); // ui.index = index of the clicked tab
}
});
$('.Button').live('click', function() {
//Here I want to reload the current selected tab
loadTab($(".Tabs").tabs('option', 'selected'));
return false;
});
$('.Tabs').tabs('select',0);
});
function loadTab(indx){
$.ajax({
type: "GET",
url: "http://domain.com/Service.svc/get",
dataType: "json",
success: function(data) {
var content = "";
$.each(data.items, function(items){
content += "<a class='Button' href='#'>" + this.title + "</a><br>";
});
$("#Content").html(content + "<br />Tab Index #" + indx + " on " + (new Date()).toUTCString());
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (!$('#error').length) $("#Content").prepend('<div id="error"></div>');
$('#error').html(textStatus + '<br /><br />');
}
})
}
只是为了澄清,这是jQuery UI的选项卡或另一个插件?另外,当你说“标签内的按钮”时,你的意思是该标签的内容区域有一个按钮,当你点击它时,你想要重新加载内容,对吧? – jbnunn 2009-12-12 22:31:04
是的!对不起,我不清楚,我更新了这个问题。我对jQuery相当新,所以也许我做错了什么。你有一个想法如何解决它? – Martin 2009-12-12 23:31:38