我有以下代码。问题是,当我在单击函数内警告id和status的值时,它工作正常,但是当我尝试在那之外执行此操作时,id的值最初是未定义的,但警报在此之后不起作用。请帮忙
var status = 1;
var id;
$("body").on('click','#first' , function() {
id = 1;
$.ajax({
type: "POST",
url: "url",
data: {id: id, status: status},
dataType: "html",
success: function(data) {
//alert(data);
if($.trim(data) == "A") {
alert('A');
} else if($.trim(data) == "B") {
alert('B');
}
}, error: function() {
alert("ERROR!");
}
});
});
$("body").on('click','#second' , function() {
id = 2;
status = 0;
$.ajax({
type: "POST",
url: "url",
data: {id: id, status: status},
dataType: "html",
success: function(data) {
//alert(data);
if($.trim(data) == "A") {
alert('A');
} else if($.trim(data) == "B") {
alert('B');
}
}, error: function() {
alert("ERROR!");
}
});
});
答案 0 :(得分:0)
试试这个
var status = 1;
var id;
$("body").on('click','#first' , function() {
id = 1;
SomethingName();
});
$("body").on('click','#second' , function() {
id = 2;
status = 0;
SomethingName();
});
function SomethingName(){
$.ajax({
type: "POST",
url: "url",
data: {id: id, status: status},
dataType: "html",
success: function(data) {
//alert(data);
if($.trim(data) == "A") {
alert('A');
} else if($.trim(data) == "B") {
alert('B');
}
}, error: function() {
alert("ERROR!");
}
});
}
或试试这个
var status = 1;
var id;
$("body").on('click','#first' , function() {
id = 1;
SomethingName(id,status);
});
$("body").on('click','#second' , function() {
id = 2;
status = 0;
SomethingName(id,status);
});
function SomethingName(_id,_status){
$.ajax({
type: "POST",
url: "url",
data: {id: _id, status: _status},
dataType: "html",
success: function(data) {
//alert(data);
if($.trim(data) == "A") {
alert('A');
} else if($.trim(data) == "B") {
alert('B');
}
}, error: function() {
alert("ERROR!");
}
});
}