Javascript 未捕获的SyntaxError:missing)在完全正确的参数列表之后
我使用Javascript发送Ajax请求,并使用返回的数据创建一个带有onclick事件的“a”标记,该事件调用另一个带有参数的方法。这是代码实现:Javascript 未捕获的SyntaxError:missing)在完全正确的参数列表之后,javascript,asp.net,ajax,Javascript,Asp.net,Ajax,我使用Javascript发送Ajax请求,并使用返回的数据创建一个带有onclick事件的“a”标记,该事件调用另一个带有参数的方法。这是代码实现: function loadHistory() { var detailsForGet = { userId: sessionStorage.getItem("userName") } $.ajax({ type: "GET",
function loadHistory() {
var detailsForGet = {
userId: sessionStorage.getItem("userName")
}
$.ajax({
type: "GET",
url: "https://localhost:44326/User/getTwitterSearchHistory",
data: detailsForGet,
success: function (data) {
jQuery.each(data, function (index, item) {
console.log(item)
$('<a href="#" onclick="historySearch(' + item + ')">' + item + '</a><br/>').appendTo('#forHistory');
});
}
});
}
function historySearch(text) {
console.log(text)
document.getElementById('tweetSearching').innerHTML = "Searching...";
$.getJSON("https://localhost:44326/api/Twitter/GetSearchResults?searchString=" + text)
.done(function (data) {
if (data.length == 0) {
document.getElementById('tweetSearching').innerHTML = "No matches Found!";
}
else {
document.getElementById('tweetSearching').innerHTML = "Found " + data.length + " matches!";
console.log(data)
document.getElementById('searchResults').innerHTML = "";
$('<h3 style="text-align:center; color: white">Search Results</h3> <hr/>').appendTo('#searchResults');
$.each(data, function (key, item) {
$('<ul style="list-style:none;"><li><span style="font-weight: bold; color: white;">Post </span><span style="color: white;">' + item.FullText + '</span><br/><span style="font-weight: bold; color: white;">Uploader</span><span style="color: white;"> ' + item.CreatedBy.Name + " @" + item.CreatedBy.ScreenName + '</span><br/><span style="font-weight: bold; color: white;">Date</span><span style="color: white;"> ' + formatDateTwitter(item.CreatedAt) + '</span><li/><hr/></ul>').appendTo('#searchResults');
});
}
});
}
函数加载历史(){
变量详细信息目标={
userId:sessionStorage.getItem(“用户名”)
}
$.ajax({
键入:“获取”,
url:“https://localhost:44326/User/getTwitterSearchHistory",
数据:详细信息目标,
成功:功能(数据){
每个(数据、函数(索引、项){
console.log(项目)
$('
')。附录('#for history');
});
}
});
}
函数历史搜索(文本){
console.log(文本)
document.getElementById('tweetSearching').innerHTML=“搜索…”;
$.getJSON(“https://localhost:44326/api/Twitter/GetSearchResults?searchString=“+文本)
.完成(功能(数据){
如果(data.length==0){
document.getElementById('tweetSearching').innerHTML=“未找到匹配项!”;
}
否则{
document.getElementById('tweetSearching').innerHTML=“find”+data.length+“matches!”;
console.log(数据)
document.getElementById('searchResults')。innerHTML=“”;
$('Search Results
')。附加到('Search Results');
$。每个(数据、功能(键、项){
$(“- Post'+item.FullText+”
Uploader'+item.CreatedBy.Name+“@”+item.CreatedBy.ScreenName+”
Date+formatDateTwitter(item.CreatedAt)+'
现在,当我尝试按下“a”标记时,我得到了错误
参数列表后未捕获的语法错误(缺少)
现在,我注意到只有当我在onclick函数中调用一个带参数的方法时才会发生这种情况,因为之前我尝试调用一个没有参数的函数,但它进入了它的内部。但是,当我试图传递一个参数时,它返回该错误
我在这里没有看到什么吗?如果
项是字符串,则需要在onclick JavaScript中用引号括起该值。由于您已经在使用单引号构建HTML字符串,因此可以添加编码为\x27
的单引号
$('<a href="#" onclick="historySearch(\x27' + item + '\x27)">' + item + '</a><br/>').appendTo('#forHistory');
$('
')。附录('#for history');
另见: