我有点困惑,我怎么能把我的三个函数混合在一起,使它变短
I am little bit confused how can i mix my three function together to make it short
我需要一个如何将所有这些函数连接在一起的想法,它们只有很小的区别,那就是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 () {}
})
}
相关文章:
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- 在 Body Onload 上调用三个 Javascript 函数
- 我在 javascript 中使用了多个按钮,单击其中三个按钮时如何运行一个函数
- 我将如何创建一个函数,该函数将三个数字作为参数并返回其中最大的一个
- 无法渲染在 jQuery $.get 请求函数中添加到场景中的三个 js 对象
- 是否可以通过添加新参数而不使用 eval 将所有这三个函数合并为一个函数
- 如何创建和调用接受三个参数(页面长度)并计算块面积的 JavaScript 函数
- 如何在第三个函数中使用从两个不同函数生成的数据
- I'我试图创建一个函数,将范围(0-100)内的三个数字随机化,并打印最大的一个
- 使用javascript onclick函数更改三个图像
- Angular 1.4.8/JS:创建构造函数并将属性继承到第三个对象中
- jQuery:var(s)集,用于三个函数
- 这个dat.gui添加函数中第三个参数的含义是什么
- 使用第三个Javascript文件将JSTL传递给Javascript函数
- 我有点困惑,我怎么能把我的三个函数混合在一起,使它变短
- 如何在JavaScript中的表单中每三个字符添加一个空格并具有替换函数
- 函数只看到三个参数
- 如何对三个不同的变量使用相同的函数
- 如何使用这三个JavaScript函数参数
- 从同一IP地址点击第三个链接调用JS函数