0

我需要一个想法,如何将所有这些功能结合在一起,它们只有很小的区别,有buyer1 buyer2 buyer3什么想法吗?预先感谢

这会很有帮助,因为我有很多看起来相似的代码,而 js packer 没有多大帮助

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 () {}
    })
}
4

4 回答 4

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");
于 2013-07-10T20:29:45.243 回答
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) }
...

或者你可以直接调用新的买家函数。

于 2013-07-10T20:29:35.300 回答
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 +
于 2013-07-10T20:29:45.267 回答
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 () {}
    })
}
于 2013-07-10T20:31:42.467 回答