0

I have a form, and my JS is validating input upon submission. I have some test data that should only appear in the console if 'test=true' is in the query string. However this data is showing up despite that not being the case in some browsers.

Works: FF; Does not work: 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"), "---- ");}
}

Here's the output:

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

As you can see, location.search is nothing and 'test' is equal to false, so nothing should be logged, right? Am I missing something here?

4

1 回答 1

3

According to the documentation on jQuery.contains, it should be used to "check to see if a DOM element is a descendant of another DOM element", not comparing strings.

You can use String.indexOf instead:

if(location.search.indexOf("test=true") != -1){
于 2013-06-24T18:37:44.933 回答