Javascript未捕获的语法错误意外标识符错误 [英] Javascript uncaught syntaxerror unexpected identifier error

查看:288
本文介绍了Javascript未捕获的语法错误意外标识符错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,该方法打印一个包含值的列表,当我点击某个值时,出现以下错误:Uncaught SyntaxError:第6行上的意外标识符。

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.

结果,我无法将我点击的值传递给liveSearch方法。我试图寻找网络,希望解决这个错误,但无济于事,我找不到解决方案。请指教。

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);
}


推荐答案

在这一行

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

以及具体位置'href =javascript:liveSearch('+ arrOfSuggestText [i] +')>'你尝试创建调用函数,但是如果你看到这个字符串的值,对于 arrOfSuggestText [i] =='qwe'你可以看到类似

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)"

和浏览器引发错误 qwe

所以你只需要添加引号就好了

So you need just add quotes around like

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

这篇关于Javascript未捕获的语法错误意外标识符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆