HTML警报框在条件下弹出

时间:2019-02-19 01:32:15

标签: python html css django

在我的django项目中,如果view.py的某些输出等于某个数字,我想在html页面中构建一个函数以显示警报框。就像,单击按钮后,view.py函数执行计算并给出输出,如果该数字等于html函数中的一个值,它将显示警报。我该怎么办呢?这是我的代码,我尝试但无法显示该框。 这是html中的按钮,它还显示了流程输出“数字”:

<form action="{% url 'xx:aaa' %}" method='GET'><button type='submit' onclick = "Warn();">start</form>
<p id="name"> number </p>

在view.py函数中,aaa将处理数据并给出一个输出编号,如下所示。

def aaa(request):
    xxxx
    return number
    context = {"number": number}
    return render(request, "xxx/xxx.html", context)

在html中,我添加了一个Java脚本来处理警报框。

<script type = "text/javascript">
    var a = '0';
    document.getElementById('name').value = b;
         <!--
            function Warn() {
            if(a==b)
               alert ("This is a warning message!");
            }
         //-->
</script>

1 个答案:

答案 0 :(得分:2)

更改

<p id="name"> number </p> 

<p id="name"> {{ number }} </p> 

然后在您的JavaScript中:

<script type = "text/javascript">
    var a = '0';
    var b = '{{ number }}';
    function Warn() {
        if(a == b)
        alert ("This is a warning message!");
    }
</script>
相关问题