Javascript字符串比较不起作用

时间:2015-12-03 17:06:19

标签: javascript jquery

 $.ajax({
   url: GetRolesByRegionIdUrl,
   type: "POST",
   data: {
     "regionId": regionId
   },

   success: function(result) {
     alert(result);
     $("#divManageUserRoles ul li").each(function(i) {
       var liname = $(this).attr('rolename');

       $.each(result, function(key, value) {

         if (value.Name === liname) {
           $('li[rolename=' + liname + ']').hide();
         }
       });
     });
   },
   error: function(xhr, status, error) {
     alert(error);
   }
 });
  

我想比较if(value.Name === liname),每次都是真的我   也尝试使用==

HTML

<div id="divManageUserRoles">

  <div class="sectionHeader">
    <div class="divider-horizontal"></div>
    Rights &amp; Roles
  </div>
  <div class="form-group">
    <div class="col-xs-4">
      <label for="Roles" class="control-label">Roles</label>
    </div>
    <div class="col-xs-8">
      <ul>
        <li rolename="AssignCases">
          <input type="checkbox" value="true" name="Roles[0].IsApplied" id="Roles_0__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[0].IsApplied">
          <label class="control-label">AssignCases</label>
          <input type="hidden" value="AssignCases" name="Roles[0].Role" id="Roles_0__Role">
        </li>
        <li rolename="Audit">
          <input type="checkbox" value="true" name="Roles[1].IsApplied" id="Roles_1__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[1].IsApplied">
          <label class="control-label">Audit</label>
          <input type="hidden" value="Audit" name="Roles[1].Role" id="Roles_1__Role">
        </li>
        <li rolename="DistrictAdmin">
          <input type="checkbox" value="true" name="Roles[2].IsApplied" id="Roles_2__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[2].IsApplied">
          <label class="control-label">DistrictAdmin</label>
          <input type="hidden" value="DistrictAdmin" name="Roles[2].Role" id="Roles_2__Role">
        </li>
        <li rolename="OfficeAdmin">
          <input type="checkbox" value="true" name="Roles[3].IsApplied" id="Roles_3__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[3].IsApplied">
          <label class="control-label">OfficeAdmin</label>
          <input type="hidden" value="OfficeAdmin" name="Roles[3].Role" id="Roles_3__Role">
        </li>
        <li rolename="ReadOnly">
          <input type="checkbox" value="true" name="Roles[4].IsApplied" id="Roles_4__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[4].IsApplied">
          <label class="control-label">ReadOnly</label>
          <input type="hidden" value="ReadOnly" name="Roles[4].Role" id="Roles_4__Role">
        </li>
        <li rolename="RegionAdmin">
          <input type="checkbox" value="true" name="Roles[5].IsApplied" id="Roles_5__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[5].IsApplied">
          <label class="control-label">RegionAdmin</label>
          <input type="hidden" value="RegionAdmin" name="Roles[5].Role" id="Roles_5__Role">
        </li>
        <li rolename="Security">
          <input type="checkbox" value="true" name="Roles[6].IsApplied" id="Roles_6__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[6].IsApplied">
          <label class="control-label">Security</label>
          <input type="hidden" value="Security" name="Roles[6].Role" id="Roles_6__Role">
        </li>
        <li rolename="Unit Admin">
          <input type="checkbox" value="true" name="Roles[7].IsApplied" id="Roles_7__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[7].IsApplied">
          <label class="control-label">Unit Admin</label>
          <input type="hidden" value="Unit Admin" name="Roles[7].Role" id="Roles_7__Role">
        </li>
        <li rolename="Worker">
          <input type="checkbox" value="true" name="Roles[8].IsApplied" id="Roles_8__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[8].IsApplied">
          <label class="control-label">Worker</label>
          <input type="hidden" value="Worker" name="Roles[8].Role" id="Roles_8__Role">
        </li>
        <li rolename="Worker1">
          <input type="checkbox" value="true" name="Roles[9].IsApplied" id="Roles_9__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[9].IsApplied">
          <label class="control-label">Worker1</label>
          <input type="hidden" value="Worker1" name="Roles[9].Role" id="Roles_9__Role">
        </li>
        <li rolename="Worker2">
          <input type="checkbox" value="true" name="Roles[10].IsApplied" id="Roles_10__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[10].IsApplied">
          <label class="control-label">Worker2</label>
          <input type="hidden" value="Worker2" name="Roles[10].Role" id="Roles_10__Role">
        </li>
        <li rolename="Worker4">
          <input type="checkbox" value="true" name="Roles[11].IsApplied" id="Roles_11__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[11].IsApplied">
          <label class="control-label">Worker4</label>
          <input type="hidden" value="Worker4" name="Roles[11].Role" id="Roles_11__Role">
        </li>
        <li rolename="Worker6">
          <input type="checkbox" value="true" name="Roles[12].IsApplied" id="Roles_12__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[12].IsApplied">
          <label class="control-label">Worker6</label>
          <input type="hidden" value="Worker6" name="Roles[12].Role" id="Roles_12__Role">
        </li>
        <li rolename="Worker8">
          <input type="checkbox" value="true" name="Roles[13].IsApplied" id="Roles_13__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[13].IsApplied">
          <label class="control-label">Worker8</label>
          <input type="hidden" value="Worker8" name="Roles[13].Role" id="Roles_13__Role">
        </li>
      </ul>
    </div>
  </div>
</div>

3 个答案:

答案 0 :(得分:2)

这似乎是一个案例问题。比较小写值可能会解决一些问题。

if (value.Name.toLowerCase() === liname.toLowerCase()) {
    // (...)
}

答案 1 :(得分:1)

尝试使用单$.each().filter()

var result = [{
  Name: "AssignCases"
}, {
  Name: "Audit"
}, {
  Name: "Worker2"
}];

$.each(result, function(key, value) {
  $("#divManageUserRoles ul li").filter("[rolename=" + value.Name + "]").hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div id="divManageUserRoles">

  <div class="sectionHeader">
    <div class="divider-horizontal"></div>
    Rights &amp; Roles
  </div>
  <div class="form-group">
    <div class="col-xs-4">
      <label for="Roles" class="control-label">Roles</label>
    </div>
    <div class="col-xs-8">
      <ul>
        <li rolename="AssignCases">
          <input type="checkbox" value="true" name="Roles[0].IsApplied" id="Roles_0__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[0].IsApplied">
          <label class="control-label">AssignCases</label>
          <input type="hidden" value="AssignCases" name="Roles[0].Role" id="Roles_0__Role">
        </li>
        <li rolename="Audit">
          <input type="checkbox" value="true" name="Roles[1].IsApplied" id="Roles_1__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[1].IsApplied">
          <label class="control-label">Audit</label>
          <input type="hidden" value="Audit" name="Roles[1].Role" id="Roles_1__Role">
        </li>
        <li rolename="DistrictAdmin">
          <input type="checkbox" value="true" name="Roles[2].IsApplied" id="Roles_2__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[2].IsApplied">
          <label class="control-label">DistrictAdmin</label>
          <input type="hidden" value="DistrictAdmin" name="Roles[2].Role" id="Roles_2__Role">
        </li>
        <li rolename="OfficeAdmin">
          <input type="checkbox" value="true" name="Roles[3].IsApplied" id="Roles_3__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[3].IsApplied">
          <label class="control-label">OfficeAdmin</label>
          <input type="hidden" value="OfficeAdmin" name="Roles[3].Role" id="Roles_3__Role">
        </li>
        <li rolename="ReadOnly">
          <input type="checkbox" value="true" name="Roles[4].IsApplied" id="Roles_4__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[4].IsApplied">
          <label class="control-label">ReadOnly</label>
          <input type="hidden" value="ReadOnly" name="Roles[4].Role" id="Roles_4__Role">
        </li>
        <li rolename="RegionAdmin">
          <input type="checkbox" value="true" name="Roles[5].IsApplied" id="Roles_5__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[5].IsApplied">
          <label class="control-label">RegionAdmin</label>
          <input type="hidden" value="RegionAdmin" name="Roles[5].Role" id="Roles_5__Role">
        </li>
        <li rolename="Security">
          <input type="checkbox" value="true" name="Roles[6].IsApplied" id="Roles_6__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[6].IsApplied">
          <label class="control-label">Security</label>
          <input type="hidden" value="Security" name="Roles[6].Role" id="Roles_6__Role">
        </li>
        <li rolename="Unit Admin">
          <input type="checkbox" value="true" name="Roles[7].IsApplied" id="Roles_7__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[7].IsApplied">
          <label class="control-label">Unit Admin</label>
          <input type="hidden" value="Unit Admin" name="Roles[7].Role" id="Roles_7__Role">
        </li>
        <li rolename="Worker">
          <input type="checkbox" value="true" name="Roles[8].IsApplied" id="Roles_8__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[8].IsApplied">
          <label class="control-label">Worker</label>
          <input type="hidden" value="Worker" name="Roles[8].Role" id="Roles_8__Role">
        </li>
        <li rolename="Worker1">
          <input type="checkbox" value="true" name="Roles[9].IsApplied" id="Roles_9__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[9].IsApplied">
          <label class="control-label">Worker1</label>
          <input type="hidden" value="Worker1" name="Roles[9].Role" id="Roles_9__Role">
        </li>
        <li rolename="Worker2">
          <input type="checkbox" value="true" name="Roles[10].IsApplied" id="Roles_10__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[10].IsApplied">
          <label class="control-label">Worker2</label>
          <input type="hidden" value="Worker2" name="Roles[10].Role" id="Roles_10__Role">
        </li>
        <li rolename="Worker4">
          <input type="checkbox" value="true" name="Roles[11].IsApplied" id="Roles_11__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[11].IsApplied">
          <label class="control-label">Worker4</label>
          <input type="hidden" value="Worker4" name="Roles[11].Role" id="Roles_11__Role">
        </li>
        <li rolename="Worker6">
          <input type="checkbox" value="true" name="Roles[12].IsApplied" id="Roles_12__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[12].IsApplied">
          <label class="control-label">Worker6</label>
          <input type="hidden" value="Worker6" name="Roles[12].Role" id="Roles_12__Role">
        </li>
        <li rolename="Worker8">
          <input type="checkbox" value="true" name="Roles[13].IsApplied" id="Roles_13__IsApplied" data-val-required="The IsApplied field is required." data-val="true">
          <input type="hidden" value="false" name="Roles[13].IsApplied">
          <label class="control-label">Worker8</label>
          <input type="hidden" value="Worker8" name="Roles[13].Role" id="Roles_13__Role">
        </li>
      </ul>
    </div>
  </div>
</div>

答案 2 :(得分:-1)

可以尝试eval(value.Name) (value.Name+"") == liname

我认为你已经警告/控制台记录了两个值,直观地看到它们比较了吗?