2013-07-10 13 views
0

我需要一個想法如何我可以加入所有這些功能在一起,他們只有一點區別是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() {} 
    }) 
} 
+7

http://codereview.stackexchange.com/ – lifetimes

+2

我會停下來想使它短具有那些瘋狂的變量名'F','N','z','t'去除 – PeeHaa

+2

創建方法稱爲買方並傳遞字符串參數。然後,您可以將buyer1,buyer2或buyer3傳入該函數並動態替換您正在創建的url中的值。 –

回答

4

使用的參數爲您buyer功能,我把它命名爲buyerId

function buyer(buyerId) { 
    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=buyer" + buyerId + "&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() {} 
    }) 
} 

所以現在,

buyer1 = function() { buyer(1) } 
buyer2 = function() { buyer(2) } 
... 

Ø你可以直接調用新的買家功能。

5

通過買方ID作爲字符串的函數作爲參數:

function buyerX(buyer) { 
    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=" + buyer + "&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() {} 
    }) 
} 

buyerX("buyer1"); 
buyerX("buyer2"); 
buyerX("buyer3"); 
3

傳遞參數到函數來指示的買方編號。 。 。

function buyer(index) { 
    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=buyer" + index + "&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 buyer(index) { 

。 。 。和。 。 。

data: "action=buyer" + index + "&pet_id=" + f + "&my_id=" + n + "&token=" + z + 
0

似乎唯一不同的是正在傳遞的action參數值。也許你可以簡單地將該值傳遞給函數。

function buyer(action) { 
    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="+action+"&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() {} 
    }) 
}