Javascript 预期表达式,脚本结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29091060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected expression, got end of script
提问by FatalKeystroke
So I have the below code in the header of a webpage:
所以我在网页的标题中有以下代码:
<script type="text/javascript">
var counter = 2;
function addNewItemField(divName) {
var newDiv = document.createElement("div");
newDiv.id = "item_listing_" + counter;
newDiv.innerHTML = "<label for=\"item_" + counter + "\">Item: </label><br />";
newDiv.innerHTML += "<input type=\"text\" id=\"item_" + counter + "_category\" list=\"list_categories\" name=\"items[]\">";
newDiv.innerHTML += "<input type=\"number\" id=\"item_" + counter + "_amount\" mine=\"0.00\" step=\"0.01\" value=\"0.00\" name=\"amounts[]\"><br />";
document.getElementById(divName).appendChild(newDiv);
counter++;
}
</script>
I try calling it using a button, however I always get a syntax error stating "expected expression, got end of script."
我尝试使用按钮调用它,但是我总是收到一个语法错误,指出“预期表达式,脚本结束”。
I've ran it through Linter and it found no errors, I've reviewed it a hundred times and I cannot find where the fault is. I hate to just post code and ask "Why doesn't this work?" but I don't have any idea what's going on so I'm at a loss on even how to ask a proper question for it.
我已经通过 Linter 运行它并没有发现任何错误,我已经查看了一百次,但我找不到问题所在。我讨厌只是发布代码并问“为什么这不起作用?” 但我不知道发生了什么,所以我什至不知道如何提出正确的问题。
UPDATE
更新
Here is the associated piece of HTML furhter down the page where the function call is made and the peices being manipulated reside:
这是在进行函数调用和被操纵的文件所在的页面下方的相关 HTML 片段:
<div id="item_listings">
<div id="item_listing_1">
<label for="item_1">Item: </label><br />
<input type="text" id="item_1_category" list="list_categories" name="items[]">
<input type="number" id="item_1_amount" min="0.00" step="0.01" value="0.00" name="amounts[]"><br />
</div>
</div>
<br />
<input id= "add_new_item" type="button" onClick="javascript:addNewItemField("item_listings")" value="Add Another Item">')
回答by meagar
onClick="javascript:addNewItemField("item_listings")"is full of errors.
onClick="javascript:addNewItemField("item_listings")"充满了错误。
You cannot mix-and-match double quotes that way. You need to use single quotes inside the double quotes or you're just stopping your HTML element's attribute early.
您不能以这种方式混合和匹配双引号。您需要在双引号内使用单引号,或者您只是提前停止 HTML 元素的属性。
Right now, this parses as
现在,这解析为
<input id= "add_new_item" type="button" onClick="javascript:addNewItemField("
... followed by a bunch of garbage.
……后面是一堆垃圾。
You need to use single quotes:
您需要使用单引号:
<input id= "add_new_item" type="button" onClick="javascript:addNewItemField('item_listings')" value="Add Another Item">

