【问题标题】:jQuery, if not equal operator [duplicate]jQuery,如果不等于运算符[重复]
【发布时间】:2022-09-26 15:19:28
【问题描述】:

这一定很愚蠢,但我不能让工作成为我的“不等于”运算符。

if(data[\'bic\'] != \'\'){
    console.log(data[\'bic\'].length);

    if(data[\'bic\'].length != 8 || data[\'bic\'].length != 11){
        var error = 1;
        alert(\'Attention, BIC must contain 8 or 11 characters\');
        $( \'label[for=\"bic\"]\' ).css(\'color\', \'red\');
    }
}

console.log() 给我 8 但我得到了警报()。 它不应该进入我的 if,我不明白我做错了什么。

  • 呃...\'data[\'bic\'].length != 8 || data[\'bic\'].length != 11\' 如果 length!=8 将为真或者长度!= 11,因此 8!= 11,你在你的 if

标签: jquery


【解决方案1】:

您需要使用&& 而不是||,因为data['bic'].length != 11 是正确的,并且如果代码块将让您的代码进入

if(data['bic'] != ''){
    console.log(data['bic'].length);

    if(data['bic'].length != 8 && data['bic'].length != 11){
        var error = 1;
        alert('Attention, BIC must contain 8 or 11 characters');
        $( 'label[for="bic"]' ).css('color', 'red');
    }
}

【讨论】:

  • 这个问题是经常重复一遍,我们有一个完善的欺骗目标。
  • @T.J.Crowder 对不起,我只是想赢得一些声誉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 2018-03-16
  • 2011-06-24
  • 2015-08-04
  • 2011-05-04
  • 2012-09-03
  • 2016-07-29
相关资源
最近更新 更多