JS测试输出显示在控制台中,而它应该'不要使用Chrome和IE

JS Test output showing up in console when it shouldn't be in Chrome and IE

本文关键字:IE Chrome 显示 输出 测试 控制台 JS      更新时间:2023-09-26

我有一个表单,我的JS在提交时验证输入。我有一些测试数据,只有当"test=true"在查询字符串中时,这些数据才会出现在控制台中。然而,尽管在某些浏览器中并非如此,但这些数据还是显示出来了。

作品:FF;不工作:Chrome,IE

$.each($(".required"), function(){
    var test = false;
    if($.contains(location.search.toString(),"test=true")){
        console.log("test activated");
        console.log("location.search: ",location.search);
        console.log("test: ",test)
        test = true;
    }
            console.log("test after: ",test);
    if(test){console.log("---- ", $(this).attr("name"), "---- ");}
}

这是输出:

test activated 
location.search:   
test:  false 
--------------  fname ------------ 

正如你所看到的,location.search不算什么,"test"等于false,所以不应该记录任何内容,对吧?我是不是遗漏了什么?

根据jQuery.contains的文档,它应该用于"检查DOM元素是否是另一个DOM元素的后代",而不是比较字符串。

您可以使用String.indexOf代替:

if(location.search.indexOf("test=true") != -1){