非常简单地说,在我的GET方法的ajaxSuccess部分中,为了检索数据,我有12 if
这样:
$(document).ready(function () {
$.ajax({
contentType: "application/json; charset=utf-8",
type: 'GET',
url: 'api/Appointments/',
dataType: 'json',
success: function (result) {
if ((result.AppTime == "9:00") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A9").style.background = "red";
}
if ((result.AppTime == "9:30") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A930").style.background = "red";
}
if ((result.AppTime == "10:00") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A930").style.background = "red";
}
if ((result.AppTime == "10:30") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A930").style.background = "red";
}
if ((result.AppTime == "11:00") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A930").style.background = "red";
}
else {
alert("error1");
}
},
error: function (error) {
alert("error");
},
});
});
CONTROLLER:
private AjanvarusEntities2 db = new AjanvarusEntities2();
[HttpGet]
public List<AppointmentsDT> ReturnApps()
{
List<AppointmentsDT> list = new List<AppointmentsDT>();
foreach (Appointment a in db.Appointments)
{
AppointmentsDT a1 = new AppointmentsDT();
a1.AppWithYritys = a.AppWithYritys;
a1.AppTime = a.AppTime;
list.Add(a1);
}
return list;
}
取决于从数据库中检索到的数据,它应该为相应的div
着色,正如您所看到的那样,但它为所有div
着色,我知道唯一的存在在数据库中,可以检索的是第一个if,只有第一个if为true。我做错了吗?
修改
我添加了控制器和整个ajax,以便更深入地了解代码。
答案 0 :(得分:0)
您的IF
语句中似乎使用了错误的运算符。您应该使用==
代替=
success: function (result) {
if ((result.AppTime == "9:00") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A9").style.background = "red";
}
if ((result.AppTime == "9:30") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A930").style.background = "red";
}
if ((result.AppTime == "10:00") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A930").style.background = "red";
}
if ((result.AppTime == "10:30") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A930").style.background = "red";
}
if ((result.AppTime == "11:00") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A930").style.background = "red";
}
另外,我认为您的情况可以进一步简化:
success: function (result) {
if ((result.AppTime == "9:00") && (result.AppWithYritys == "Laakkonen")) {
document.getElementById("A9").style.background = "red";
}
if ((result.AppTime == "9:30" || result.AppTime == "10:00" || result.AppTime = "10:30" || result.AppTime = "11:00") && (result.AppWithYritys = "Laakkonen")) {
document.getElementById("A930").style.background = "red";
}
更好的是,如果result.AppWithYritys
中只有两个以上的可能结果,则可以编写嵌套开关:
switch (result.AppWithYritys) {
case: "Laakkonen": {
switch(result.Apptime) {
case "9:00":
document.getElementById("A9").style.background = "red";
break;
case "9:30":
document.getElementById("A930").style.background = "red";
break;
........
}
}
case "Other_case2": {
//switch-case statement again here for result.AppTime?
}
......
//other cases for result.AppWithYritys
}
答案 1 :(得分:0)
正如评论
中所述应使用相等的比较运算符来测试回复中的字段是否具有特定值。如果某些情况下在JavaScript中执行的自动类型转换会产生意外结果,那么在类型转换等于运算符'=='和严格相等运算符===
之间可以选择。
将对象属性或命名变量设置为非空字符串的赋值表达式在条件语句中用作布尔值时始终求值为true
。
一个新问题
在上次更新问题后注意到,标识为A930
的HTML元素也用于突出显示10:00,10:30,11:00约会。这几乎肯定不是预期的结果。
建议,用于生成可能有帮助的特定错误和警告消息的代码如下所示。请注意,它尚未经过测试。
var timeId = {
"9:00": "A900",
"9:30": "A930",
"10:00": "A1000",
"10:30": "A1030",
"11:00": "A1100" //, and so
};
var id = timeId[ reply.AppTime]; // validate appointment time
if( !id) {
console.log("Unknown appointment time: " + reply.AppTime);
}
else if( reply.AppWithYritys != "Laakkonen") {
console.log("AppWithYritys location is: " + result.AppWithYritys);
}
else {
document.getElementById( id).style.background = "red";
}