我需要一個想法如何我可以加入所有這些功能在一起,他們只有一點區別是buyer1 buyer2 buyer3
任何想法?預先感謝我有點困惑我怎麼能混合我的三個功能在一起,使它短
這將是有益的,因爲我有很多代碼,類似於和js打包機沒有幫助太多
function buyer1() {
var f = $("#buy_id").val();
var n = $("#buyer_id").val();
var z = $("#token_id").val();
var t = $("#buy_value").val();
$.ajax({
url: "ajax.php",
type: "post",
data: "action=buyer1&pet_id=" + f + "&my_id=" + n + "&token=" + z + "&buy_value=" + t + "",
dataType: "json",
success: function (e) {
token_id = e.token_id;
message = e.message;
value = e.value;
$("#buy_value").val(value);
$("#token_id").val(token_id);
$("#buyerdialog").fadeIn(300);
$("#buyerresult").html(message);
},
error: function() {}
})
}
function buyer2() {
var f = $("#buy_id").val();
var n = $("#buyer_id").val();
var z = $("#token_id").val();
var t = $("#buy_value").val();
$.ajax({
url: "ajax.php",
type: "post",
data: "action=buyer2&pet_id=" + f + "&my_id=" + n + "&token=" + z + "&buy_value=" + t + "",
dataType: "json",
success: function (e) {
token_id = e.token_id;
message = e.message;
value = e.value;
$("#buy_value").val(value);
$("#token_id").val(token_id);
$("#buyerdialog").fadeIn(300);
$("#buyerresult").html(message);
},
error: function() {}
})
}
function buyer3() {
var f = $("#buy_id").val();
var n = $("#buyer_id").val();
var z = $("#token_id").val();
var t = $("#buy_value").val();
$.ajax({
url: "ajax.php",
type: "post",
data: "action=buyer3&pet_id=" + f + "&my_id=" + n + "&token=" + z + "&buy_value=" + t + "",
dataType: "json",
success: function (e) {
token_id = e.token_id;
message = e.message;
value = e.value;
$("#buy_value").val(value);
$("#token_id").val(token_id);
$("#buyerdialog").fadeIn(300);
$("#buyerresult").html(message);
},
error: function() {}
})
}
http://codereview.stackexchange.com/ – lifetimes
我會停下來想使它短具有那些瘋狂的變量名'F','N','z','t'去除 – PeeHaa
創建方法稱爲買方並傳遞字符串參數。然後,您可以將buyer1,buyer2或buyer3傳入該函數並動態替換您正在創建的url中的值。 –