Jquery比较字符串与.val();非预期结果



在我的页面我有多个按钮(input type=button)与class=" btnDisabled",每个按钮的值是True或False。

为什么这段代码总是返回false:

 $(document).ready(function () {
             $(".btnDisabled").each(function(f,el) {
                 console.log($(this).val() == "False");

             });

         });

typeof ($(this).val())返回字符串

尝试使用$.trim()

$.trim($(this).val())=="False"

你可以粘贴你的html吗?

试试:

$(this).is(':checked')

举个例子,

 <input type="button" class="btnDisabled" value="True" />
 <input type="button" class="btnDisabled" value="False" />

In JS:

$(document).ready(function () {
  $(".btnDisabled").each(function(f,el) {
        console.log($(this).val() == "False"); 
   });
 });

在控制台中,我们可以看到true button为false, false button为true,这是正确的。请参考DEMO HERE如果你仍然不能解决这个问题,粘贴你的完整代码在你的帖子。

最新更新