【问题标题】:trying to pass one page data to next page but unfortunately data is not displayed试图将一页数据传递到下一页,但不幸的是数据未显示
【发布时间】:2020-03-30 15:08:25
【问题描述】:

我在 Django 测验项目中工作我正在尝试在下一页显示答案,但使用 question.html 用户可以选择答案并单击提交,然后显示下一页但不显示标记我正在使用 java-script 来显示标记...我们如何解决问题?

question.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>

answers.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>

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;
}

【问题讨论】:

  • 您好,您在点击此提交答案时是否在views.py 中进行任何处理?可以在views.py中贴出对应答案的方法定义吗?另外,只是为了确认一下,这一行是否显示在下一页中? --> 恭喜!.. 你好

标签: javascript python html django


【解决方案1】:

您的代码中几乎没有错误。

answers.html 中,您必须在末尾提及脚本部分(不在 &lt;head&gt; 中)。否则,脚本将首先执行,当您从 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>

然后您还没有制作带有 id ='results' 的标签来打印标记。完成后,您将获得预期的结果。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多