2016-10-03 44 views
0

即使在相同的JavaScript去如果條件無關的值給出變量

isAdmin的值=假

的代碼不會進入條件

if(!isAdmin) 

結果,我沒有得到期望的輸出。 這就是下面好象叫在asp.net:

<span onclick="DisplayPrdWeightGrd('<%#Container.ItemIndex + 1 %>','<%# Eval("OrderId")%>','<%= Convert.ToBoolean(Utility.IsAdmin()) %>');"> 

function DisplayPrdWeightGrd(index, OrderId, isAdmin) { 
 
    $.ajax({ 
 
type: "POST", 
 
url: "../handlers/DisplayOrderDetail.ashx?OrderId=" + Orderid + "&tk=" + $.now(), 
 
contentType: "application/json; charset=utf-8", 
 
dataType: "json", 
 
success: function(data) { 
 
    if ($('#tableOrderDetail_' + index + ' tr').length <= 1) { 
 
    $.each(data, function(i, v) { 
 
     setDataOnRow(index, v, i); 
 
    }); 
 
    } 
 
}, 
 
failure: function(response) { 
 
    alert("fail"); 
 
}, 
 
error: function(response) { 
 
    alert(response); 
 
} 
 
    }); 
 

 
    function setDataOnRow(idx, v, i) { 
 

 
var totalprice; 
 
if (v.IsGST == true) { 
 
    totalprice = parseFloat(v.TotalPrice * (1.1)).toFixed(2); 
 
} else { 
 
    totalprice = parseFloat(v.TotalPrice).toFixed(2); 
 
} 
 

 
//debugger; 
 

 
var obj = $('#tableOrderDetail_' + idx + ' tr td table:last'); 
 
if (!isAdmin) { 
 
    $('#tableOrderDetail_' + idx + ' tr:last').after('<tr>' + 
 
    '<td width="25%" >' + (i + 1) + '</td>' + 
 
    '<td width="25%" class="center">' + v.ProductName + '</td>' + 
 
    '<td width="12%" class="center">' + v.NoOfCarton + '</td>' + 
 
    '<td width="12%" class="center">' + v.ProductQuantity + '</td>' + 
 
    '<td width="12%" class="center">' + v.OriginalPrice + '</td>' + 
 
    '<td width="12%" class="center">' + v.OrderPrice + '</td>' + 
 
    '<td width="10%" class="center">' + v.IsGST + '</td>' + 
 
    '<td width="10%" class="center">' + v.Discount + '</td>' + 
 
    '<td width="12%" class="center">' + totalprice + '</td></tr>'); 
 
} else { 
 
    $('#tableOrderDetail_' + idx + ' tr:last').after('<tr>' + 
 
    '<td width="25%" >' + (i + 1) + '</td>' + 
 
    '<td width="25%" class="right">' + v.ProductName + '</td>' + 
 
    '<td width="12%" class="right">' + v.NoOfCarton + '</td>' + 
 
    '<td width="12%" class="right">' + v.ProductQuantity + '</td>' + 
 
    '<td width="12%" class="right">' + v.OriginalPrice + '</td>' + 
 
    '<td width="12%" class="right">' + v.OrderPrice + '</td>' + 
 
    '<td width="10%" class="right">' + v.IsGST + '</td>' + 
 
    '<td width="10%" class="center">' + v.Discount + '</td>' + 
 
    '<td width="12%" class="center">' + totalprice + '</td></tr>'); 
 
} 
 
    }

我試着調試器,它總是將是價值isAdmin else條件是真還是假。卡在這裏。

+0

如果我更改爲if(isAdmin),那麼它在條件而不是其他。反應是副詩 –

+0

檢查typeof isAdmin。如果是字符串,那麼它總是會進入if條件。 – Sachin

+0

確定它的類型是字符串 –

回答

0

function DisplayPrdWeightGrd(index, OrderId, isAdmin) {  var Admin = isAdmin.toLowerCase(); 
 

 
    $.ajax({ 
 
type: "POST", 
 
url: "../handlers/DisplayOrderDetail.ashx?OrderId=" + Orderid + "&tk=" + $.now(), 
 
contentType: "application/json; charset=utf-8", 
 
dataType: "json", 
 
success: function(data) { 
 
    if ($('#tableOrderDetail_' + index + ' tr').length <= 1) { 
 
    $.each(data, function(i, v) { 
 
     setDataOnRow(index, v, i); 
 
    }); 
 
    } 
 
}, 
 
failure: function(response) { 
 
    alert("fail"); 
 
}, 
 
error: function(response) { 
 
    alert(response); 
 
} 
 
    }); 
 

 
    function setDataOnRow(idx, v, i) { 
 

 
var totalprice; 
 
if (v.IsGST == true) { 
 
    totalprice = parseFloat(v.TotalPrice * (1.1)).toFixed(2); 
 
} else { 
 
    totalprice = parseFloat(v.TotalPrice).toFixed(2); 
 
} 
 

 
//debugger; 
 

 
var obj = $('#tableOrderDetail_' + idx + ' tr td table:last'); 
 
if (Admin === "false") { 
 
    $('#tableOrderDetail_' + idx + ' tr:last').after('<tr>' + 
 
    '<td width="25%" >' + (i + 1) + '</td>' + 
 
    '<td width="25%" class="center">' + v.ProductName + '</td>' + 
 
    '<td width="12%" class="center">' + v.NoOfCarton + '</td>' + 
 
    '<td width="12%" class="center">' + v.ProductQuantity + '</td>' + 
 
    '<td width="12%" class="center">' + v.OriginalPrice + '</td>' + 
 
    '<td width="12%" class="center">' + v.OrderPrice + '</td>' + 
 
    '<td width="10%" class="center">' + v.IsGST + '</td>' + 
 
    '<td width="10%" class="center">' + v.Discount + '</td>' + 
 
    '<td width="12%" class="center">' + totalprice + '</td></tr>'); 
 
} else { 
 
    $('#tableOrderDetail_' + idx + ' tr:last').after('<tr>' + 
 
    '<td width="25%" >' + (i + 1) + '</td>' + 
 
    '<td width="25%" class="right">' + v.ProductName + '</td>' + 
 
    '<td width="12%" class="right">' + v.NoOfCarton + '</td>' + 
 
    '<td width="12%" class="right">' + v.ProductQuantity + '</td>' + 
 
    '<td width="12%" class="right">' + v.OriginalPrice + '</td>' + 
 
    '<td width="12%" class="right">' + v.OrderPrice + '</td>' + 
 
    '<td width="10%" class="right">' + v.IsGST + '</td>' + 
 
    '<td width="10%" class="center">' + v.Discount + '</td>' + 
 
    '<td width="12%" class="center">' + totalprice + '</td></tr>'); 
 
} 
 
    }

它解決了我的問題,使用下面兩行: 1。在功能

功能DisplayPrdWeightGrd(索引,的OrderId,isAdmin)

添加

var Admin = isAdmin.toLowerCase(); 

2在函數

功能setDataOnRow(IDX,V,I)

如果條件加入

if(Admin==="false") 

感謝討論。

0

嘗試

var isAdmin = Boolean(isAdmin); 

然後檢查的if-else條件。

if(!isAdmin) 
+0

仍然是同樣的情況,它不在裏面如果(!isAdmin)條件塊 –

+0

@HinaKhuman轉換後console.log(typeof isAdmin,isAdmin)的輸出是什麼。 – Sachin

+0

var Admin = typeof(isAdmin);給我的字符串類型 –

2

您正在將該isAdmin參數的值設置爲字符串。因此,「if」中的檢查將檢查字符串的「truthy」值,該值是任何非空值/非空值。因此,值'True''False'將被視爲true

要解決這個問題:

  1. 除去周圍<%= Convert.ToBoolean(Utility.IsAdmin()) %>
  2. 引號確保你得到truefalse(小寫!)。您可能需要添加一個.ToString().ToLowerInvariant()。或(猜Utility.IsAdmin()返回一個布爾值):<%= Utility.IsAdmin() ? "true" : "false" %>
+0

使用你的刪除引用的點,根本不叫DisplayPrdWeightGrd –

+0

@HinaKhuman它寫出了'True'(大寫T)還是'true'(小寫)? (它應該是小寫)實際在生成的html/javascript中是什麼?在JavaScript控制檯中的任何錯誤? –

+0

雖然,我用你的第二點,並找到了答案 –

0

isAdmin只存在你的Ajax調用內,因此您setDataOnRow功能將無法識別它,因此總是去else

+0

不,它確實進入* setDataOnRow *。我在該函數中設置了alert .alert給出了確切的值 –

相關問題