我有点困惑,我怎么能把我的三个函数混合在一起,使它变短

I am little bit confused how can i mix my three function together to make it short

本文关键字:函数 三个 混合 在一起 怎么能 我的      更新时间:2023-09-26

我需要一个如何将所有这些函数连接在一起的想法,它们只有很小的区别,那就是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 () {}
    })
}

将买方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");

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) }
...

或者您可以直接调用新买家功能。

将一个参数传递到函数中以指示买家编号。

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 +

似乎唯一不同的是正在传递的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 () {}
    })
}