我将 html 代码注入(inject)到 html 中,尽管我的所有其他按钮都可以工作,但是这个将数据发送到 sqlite 数据库的太平洋按钮却出现以下错误。
未捕获的语法错误:参数列表后缺少 )
问题是我已经审查了代码并尝试了不同的方法,并且由于我注入(inject)了代码,所以它不会捕获仅指向 DOCTYPE 的错误?
正在注入(inject)的 HTML:
$(info).html('<h4 class="media-heading">' + response[rCnt].title + '</h4><h3><span class="glyphicon glyphicon-star" aria-hidden="true"> </span> ' + response[rCnt].social_rank.toFixed(2) + '</h3><h3><span class="glyphicon glyphicon-print" aria-hidden="true"> </span> ' + response[rCnt].publisher + '</h3><h3><span class="glyphicon glyphicon-home" aria-hidden="true"> </span> <a> ' + response[rCnt].source_url + '</a></h3><button type="button" class="btn btn-default" id="button1" onclick="saveRecipe(' + response[rCnt].title + ',' + response[rCnt].social_rank.toFixed(2) + ',' + response[rCnt].publisher + ',' + response[rCnt].source_url + ')" title="Save"><span class="glyphicon glyphicon-save" aria-hidden="true"></span></button>');
内部有 HTML 按钮,因此更易于阅读:
<button type="button" class="btn btn-default" id="button1" onclick="saveRecipe(' + response[rCnt].title + ',' + response[rCnt].social_rank.toFixed(2) + ',' + response[rCnt].publisher + ',' + response[rCnt].source_url + ')" title="Save"><span class="glyphicon glyphicon-save" aria-hidden="true"></span></button>
HTML 按钮提供数据的 AJAX 调用:
function saveRecipe(title, rank, auth, src) {
$.ajax({
type: "POST",
headers: {
"Content-Type": "application/json"
},
url: "/SaveFile",
data: JSON.stringify({
name: title,
rating: rank,
author: auth,
source: src
}),
success: function(response) {
console.log("Hii");
console.log(response);
},
error: function(response, error) {
console.log(response);
console.log(error);
}
});
}
最佳答案
我对此进行了很长时间的研究,看起来您需要在 onClick 中添加一些额外的引号。 例如,如果标题是“Soup”,则 onClick 正在查找名为 Soup 的变量,而不是传递“Soup”值。 这很难使用,因为那个大的 $(info).html 语句已经同时包含单引号和双引号。但我让它发挥作用:
var onclickstring = "saveRecipe('" + response[rCnt].title + "','" + response[rCnt].social_rank.toFixed(2) + "','" + response[rCnt].publisher + "','" + response[rCnt].source_url + "')";
然后像这样插入:
id="button1" onclick=' + onclickstring + ' title
关于javascript - 除了拼写错误之外,注入(inject)代码时还会有其他什么原因导致语法错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896328/