这是我的代码:
$(document).ready(function() {
cookieanimalid =($.cookie("cookieanimal"));
$.ajax({
type: 'POST',
url: "/myurl/",
data: {cookieanimalid},
success: function (data){
if (!$.trim(data)) {
animalid = $("div.dropdown-menu a:first-child").attr("value");
$("#show").html(data);
}else {
$("#show").html(data);
}
}
});
});
我想要做的是,如果数据是空的,则在同一div中发布另一个数据,其中data = animalid。他发布了我只是动物的价值,但我需要其他人。也许sintax不对?也许是在发布ajax数据之后用var做一个if else statament?有人能帮助我吗?
答案 0 :(得分:3)
成功删除if / else,然后改为:
$.ajax({
type: 'POST',
url: "/petbook/PetBook/",
data: {
cookieanimalid
},
success: function(response) { /* <-- this was called data, it is not related to data above, you could all this anything */
animalid = $("div.dropdown-menu a:first-child").attr("value");
/* here we're saying if there is data, use that and if not, use animalid */
$("#show").html(response || animalid);
}
});
<强>更新强>
你的意思是这样吗?
cookieanimalid = ($.cookie("cookieanimal"));
animalid = $("div.dropdown-menu a:first-child").attr("value");
if (!cookieanimalid) { /* if no cookieanimalid, then show animalid */
$("#show").html(animalid);
} else { /* else go and get the data */
$.ajax({
type: 'POST',
url: "/myurl/",
data: cookieanimalid,
success: function(data) {
$("#show").html(data);
}
});
}
更新2:
你的意思是这样吗?
cookieanimalid = ($.cookie("cookieanimal"));
animalid = $("div.dropdown-menu a:first-child").attr("value");
$.ajax({
type: 'POST',
url: "/myurl/",
data: cookieanimalid || animalid, /* send whichever is true, cookieanimalid first choice */
success: function(data) {
$("#show").html(data);
}
});