我有这个示例代码块,现在不断重复很多次。
$(function(loadDatabyClickA){
$('button').live('click', function(){
selectedAgeType = $(this).attr('value');
var x = {};
x.data = $('a').attr("data"); //selected item in tree (.liselected)
x.command = $('a').attr("cmd");
x.option = "x";
x.sessionid = docCookies.getItem("sessionid");
x.ageType = selectedAgeType;
x.showData = showUnderlyingData;
var action = function(result, status) {
var x_list = "";
$.each(result, function(i, val){
x_list += "<li><h3>"+val.xtitle+"</h3></li>";
});
$('#x_view').append(x_list);
};
$.post("jsoncommand", JSON.stringify(chart), action)
.error(function(){
alert('error');
});
})
})
我怎样才能提取这个块,以便我可以从另一个函数访问它?也许喜欢使用.extend()
?此块由其他函数使用,如下所示:
$(function(loadDatabyClickB){
$('button2').live('click', function(){
selectedAgeType = $(this).attr('value');
var y = {};
y.data = $('a').attr("data"); //selected item in tree (.liselected)
y.command = $('a').attr("cmd");
y.option = "y";
y.sessionid = docCookies.getItem("sessionid");
y.ageType = selectedAgeType;
y.showData = showUnderlyingData;
var action = function(result, status) {
var y_list = "";
$.each(result, function(i, val){
y_list += "<li><h3>"+val.ytitle+"</h3></li>";
});
$('#y_view').append(y_list);
};
$.post("jsoncommand", JSON.stringify(y), action)
.error(function(){
alert('error');
});
})
})
你们可以告诉我如何做到这一点,因为我不知道。我会自己简化。在此先感谢您的帮助!