Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Basically, the method prints a list with values and when I click on a value, I get this error : Uncaught SyntaxError: Unexpected identifier on line 6.

As a result,I am unable to pass the value that I click on,onto the liveSearch method. I have tried searching the net in hopes of solving the error but to no avail,I couldn't find a solution. Please advise.

function printSuggestResult(arrOfSuggestText,getRows){
    var htmlStr  = "<button id='dropdownB' href='#' class='dropdown-toggle btn btn-default'data-toggle='dropdown'>Found  <span id='resultCount' class='badge'></span> &nbsp</b></button><ul class='list-group  scrollable-menu'>";

    for(var i=0; i<arrOfSuggestText.length; i++){
        htmlStr += "<li class='list-group-item '>";

        if(arrOfSuggestText[i] != "null"){
            htmlStr +=  '<a id="searchResult'+i+'" href="javascript:liveSearch('+arrOfSuggestText[i]+')" > '+arrOfSuggestText[i]+'</a>';
        }
        htmlStr += "</li>";
    }

    htmlStr += "</ul>";

    document.getElementById('searchResultList').innerHTML = htmlStr;
        $('#resultCount').text(getRows);
    }

function liveSearch(getText){
    var arrOfText = new Array();
    var arrOfLat = new Array();
    var arrOfLon = new Array();

    getText = getText.replace(" ","+");
    var testy = encodeURIComponent(getText);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

In this line

htmlStr +=  '<a id="searchResult'+i+'" href="javascript:liveSearch('+arrOfSuggestText[i]+')" > '+arrOfSuggestText[i]+'</a>';

and concrete here '" href="javascript:liveSearch('+arrOfSuggestText[i]+')" > ' you try create calling function, but if you see value of this string, for arrOfSuggestText[i] == 'qwe' you can see something like

href="javascript:liveSearch(qwe)"

and browser raise error what you get on qwe.

So you need just add quotes around like

'" href="javascript:liveSearch(''+arrOfSuggestText[i]+'')" > '

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...