【发布时间】:2015-08-01 03:28:49
【问题描述】:
我遵循 Django 网站上的教程,并尝试在 Django 网站的教程中的 results.html 创建级联下拉列表。
我在javascript函数中使用Django标签{{ }}和{% %}时遇到了语法错误。我使用的 IDE 是 Komodo Edit,它将这一行 {% for item in question.choice_set.all %} 突出显示为红色并指出错误:Javascript: SyntaxError: expected expression, got '%'。
我想问一下,我该如何解决这个问题?
非常感谢!如下是我的 html 脚本。
results.html
<!doctype html>
<html>
<body>
<script type="text/javascript">
function change(chosen,updateList){
document.getElementById('text').value = chosen;
updateList.options.length=0;
{% for item in question.choice_set.all %}
if (item = chosen) {
updateList.options[updateList.options.length] = new Option({{item.votes}}, '')
}
{% endfor %}
}
</script>
<form name='form' action="{% url 'polls:results' quezstion.id %}", method='post'>
{% csrf_token %}
<h1>{{ question.question_text }}</h1>
<select name="Choice" onchange="change(document.form.Choice.options[document.form.Choice.selectedIndex].value, document.form.Votes)">
{% for item in question.choice_set.all %} //question used here is defined in views
<option value="{{item.id}}">{{ item.choice_text }}</option>
{% endfor %}
</select>
<select name="Votes">
<option></option>
</select>
</form>
【问题讨论】:
-
尝试在 stackoverflow 或谷歌搜索类似的答案:stackoverflow.com/questions/6008908/…
-
嗨 taesu,我在发布问题之前阅读了此内容。它显示相同的语法错误:)
-
只是为了理解,这只是IDE的错误吗?除此之外,这是否正常工作?乍一看还不错。
-
@Wtower,嗨。不,它不起作用:0
标签: javascript html django