繁体   English   中英

使用===检查两个值是否有意义地相等,不适用于javascript

[英]check if two values are meaningfully equal using === not working in javascript

我想使用JavaScript检查两个值是否有意义地相等,但是单击按钮时它不起作用。 以下是我的尝试

jQuery(document).ready(function(){
  jQuery(':button').click(function () {
    if (this.id == 'click') {
        alert('this button was clicked');

        $('input[type=text]').blur(function(){
    $(this).val($.trim($(this).val()));
        });//trim white spaces

      var name = localStorage.getItem("inputName");

    if(document.getElementById('inputName').value === name ){

    alert('both values are equal'); //but it never gets executed when the two values are equal

}else {
      alert('not showing anything'); //always executing this line
    }

    }else {

    }
});
});

仅当两个值相等时才如何执行if块。

尝试使用.val()获取值,并检查var name具有的类型

您可以为此任务进行常规比较==或进行检查解析以使用===

代码片段

 $('.click').on("click", function() { var input = $('#inputName').val(); var name = "hello"; if (input == name) { alert('both values are equal'); } else { alert('both are not equal'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="click" id="click">Click</button> <input type=text id="inputName" /> 

if(some.value === name && other.value === eman ) {
  execute();
}

那并不能解决您可能遇到的任何“侦听器”问题,但这就是您需要满足两个条件的方式(如果继续使用此模式,则要求更多)。

另外,|| 用于“或”条件。

对于特定情况,我使用了“加法”解决方案,其中两个条件均为true会将变量设置为“ 2”,只有一个条件为true会将变量设置为1,而没有一个条件为true会将变量设置为0。当只有一个条件必须为真时...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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