简体   繁体   中英

Javascript comparison help

I am having trouble with this statement and I was hoping I could get some help. Essentially, if edweek and edevenings (which are checkboxes) are not equal then I want to push to my array. The problem I am having is that I can get one of the values to be not equal, but when I add the second it doesn't work.

function getEdVals() {
  var edVals = [];
  $('#edinitiativecont :checked').each(function() {
    if($(this).val() == $("#liveweb").val()){
      edVals.push($(this).val());
    } else {
      if($(this).val() == (!($("#edweek").val()) || $("#edevenings").val())){
    edVals.push($(this).val());
      }
    }
  });
}

change

if($(this).val() == (!($("#edweek").val()) || $("#edevenings").val())){
    edVals.push($(this).val());
}

to

var val = $(this).val() ;
if (val != $("#edweek").val() && val != $("#edevenings").val()) {
  edVals.push($(this).val());
}

Without seeing everything together, it is a bit difficult to ensure an accurate solution. The solution below condenses your current logic and corrects the else conditional statement.

function getEdVals() {   
    var edVals = [];   
    $('#edinitiativecont :checked').each(function() {
        var ele = $(this);
        var eleValue = ele.val();
        if(eleValue == $("#liveweb").val() || (eleValue != $("#edweek").val() || eleValue != $("#edevenings").val())){       
            edVals.push(eleValue);     
        } 
    }); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM