Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

html - Javascript uncaught syntaxerror unexpected identifier error

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]+'')" > '

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...