Javascript 试图将一页数据传递到下一页,但不幸的是数据未显示
我正在Django测验项目中工作我正在尝试在下一页显示答案,但使用question.html用户可以选择答案并单击submit,然后显示下一页,但不显示分数我正在使用java脚本显示分数。。。我们怎样才能解决这个问题 question.htmlJavascript 试图将一页数据传递到下一页,但不幸的是数据未显示,javascript,python,html,django,Javascript,Python,Html,Django,我正在Django测验项目中工作我正在尝试在下一页显示答案,但使用question.html用户可以选择答案并单击submit,然后显示下一页,但不显示分数我正在使用java脚本显示分数。。。我们怎样才能解决这个问题 question.html <!DOCTYPE html> <html> <head> <title></title> {% load static %} <script src="{
<!DOCTYPE html>
<html>
<head>
<title></title>
{% load static %}
<script src="{% static 'JS/quiz1.js' %}"></script>
</head>
<body>
<div id = "txt"></div>
<form name="quizform" action="{% url 'answer' %}" method="POST" onsubmit="return submitanswer(answers=[{% for poll in questions %}'{{ poll.right_ans }}', {% endfor %}])">
{% csrf_token %}
{% for poll in questions %}
<h1>{{poll.question_id}}{{poll.question}}</h1>
<h3><input type="radio" name="poll{{poll.question_id}}" id="poll1a" value="{{poll.option_1}}" >a.{{poll.option_1}}</h3>
<h3><input type="radio" name="poll{{poll.question_id}}" id="poll1b" value="{{poll.option_2}}">b.{{poll.option_2}}</h3>
<h3><input type="radio" name="poll{{poll.question_id}}" id="poll1c" value="{{poll.option_3}}">c.{{poll.option_3}}</h3>
<h3><input type="radio" name="poll{{poll.question_id}}" id="poll1d" value="{{poll.option_4}}" >d.{{poll.option_4}}</h3>
{% endfor %}
<input type="Submit" value="Submit Answer" onclick="passvalues();" >
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Result</title>
<script type="text/javascript">
document.getElementById('maitrik').innerHTML=localStorage.getItem('textvalue');
</script>
</head>
<body>
congretulations!.. <span id="maitrik">Hello</span>
</body>
</html>
{%load static%}
{%csrf_令牌%}
{%用于问题中的投票%}
{{poll.question}{{poll.question}
a、 {{poll.option_1}}
b、 {{poll.option_2}}
c、 {{poll.option_3}}
d、 {{poll.option_4}}
{%endfor%}
answers.html
<!DOCTYPE html>
<html>
<head>
<title></title>
{% load static %}
<script src="{% static 'JS/quiz1.js' %}"></script>
</head>
<body>
<div id = "txt"></div>
<form name="quizform" action="{% url 'answer' %}" method="POST" onsubmit="return submitanswer(answers=[{% for poll in questions %}'{{ poll.right_ans }}', {% endfor %}])">
{% csrf_token %}
{% for poll in questions %}
<h1>{{poll.question_id}}{{poll.question}}</h1>
<h3><input type="radio" name="poll{{poll.question_id}}" id="poll1a" value="{{poll.option_1}}" >a.{{poll.option_1}}</h3>
<h3><input type="radio" name="poll{{poll.question_id}}" id="poll1b" value="{{poll.option_2}}">b.{{poll.option_2}}</h3>
<h3><input type="radio" name="poll{{poll.question_id}}" id="poll1c" value="{{poll.option_3}}">c.{{poll.option_3}}</h3>
<h3><input type="radio" name="poll{{poll.question_id}}" id="poll1d" value="{{poll.option_4}}" >d.{{poll.option_4}}</h3>
{% endfor %}
<input type="Submit" value="Submit Answer" onclick="passvalues();" >
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Result</title>
<script type="text/javascript">
document.getElementById('maitrik').innerHTML=localStorage.getItem('textvalue');
</script>
</head>
<body>
congretulations!.. <span id="maitrik">Hello</span>
</body>
</html>
结果
document.getElementById('maitrik')。innerHTML=localStorage.getItem('textvalue');
祝贺你!。。你好
quiz1.js
function submitanswer(answers)
{
var total = answers.length;
var score = 0;
var choice=[]
for(var i=1;i<=total;i++)
{
choice[i]=document.forms["quizform"]["poll"+i].value;
}
for(i=1;i<=total;i++)
{
if(choice[i]== null || choice[i] == ''){
alert('you missed questions:'+i);
return false;
}
}
//var right_answer=["a","a","a","a","a","a","a","a","a","a"]
for(i=1;i<=total;i++)
{
if(choice[i] ==answers[i-1]){
score=score+1;
}
console.log(answers[i]);
}
var results=document.getElementById('results');
results.innerHTML="<h3>Your scored is <span>" + score + "</span> out of <span>"+total +"</span></h3>"
alert("You scored" + score + "out of" +total);
return false;
}
function passvalues()
{
var firstname=document.getElementById("txt").value;
localStorage.setItem("textvalue",firstname);
return false;
}
功能提交人(答案)
{
var total=答案长度;
var得分=0;
变量选择=[]
对于(var i=1;i,代码中几乎没有错误
在answers.html中,您必须在末尾提到脚本部分(而不是
中)。否则,脚本将首先执行,从localStorage获取项目时,您将得到null
<!DOCTYPE html>
<html>
<head>
<title>Result</title>
</head>
<body>
congretulations!.. <span id="maitrik">Hello</span>
</body>
</html>
<script type="text/javascript">
document.getElementById('maitrik').innerHTML=localStorage.getItem('textvalue');
</script>
结果
恭喜!你好
document.getElementById('maitrik')。innerHTML=localStorage.getItem('textvalue');
然后你还没有做一个id='results'的标记来打印标记。一旦这样做了,你就会得到预期的结果
谢谢。您好,单击此提交答案时,您是否在views.py中进行任何处理?您是否可以发布与views.py中的答案相对应的方法定义?另外,为了确认,这一行是否显示在下一页?-->Congregations!。您好