如果条件不同于给定变量的值,则使用相同的javascript

时间:2016-10-03 09:57:19

标签: javascript asp.net

即使值

  

isAdmin = false

代码没有进入条件

if(!isAdmin)

结果,我没有获得所需的输出。 如果在asp.net中调用,则如下所示:

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

&#13;
&#13;
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>');
}
  }
&#13;
&#13;
&#13;

我试过调试器,它始终是值 isAdmin为true或false 的其他条件。被困在这里。

4 个答案:

答案 0 :(得分:2)

您将该isAdmin参数的值设置为字符串。因此,“if”内部的检查检查字符串的“truthy”值,即任何非null /非空值。因此,值'True''False'都将被视为true

解决这个问题:

  1. 删除<%= Convert.ToBoolean(Utility.IsAdmin()) %>
  2. 周围的引号
  3. 确保获得truefalse(小写!)。您可能需要添加.ToString().ToLowerInvariant()。或者(猜测Utility.IsAdmin()返回一个bool):<%= Utility.IsAdmin() ? "true" : "false" %>

答案 1 :(得分:0)

尝试

var isAdmin = Boolean(isAdmin);

然后检查if-else条件。

if(!isAdmin)

答案 2 :(得分:0)

isAdmin仅存在于您的ajax调用中,因此您的setDataOnRow函数无法识别它,因此始终转到else

答案 3 :(得分: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(index,OrderId,isAdmin)

添加

 var Admin = isAdmin.toLowerCase();

2在功能

  

function setDataOnRow(idx,v,i)

条件添加为

    if(Admin==="false")

感谢您的讨论。