Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从html按钮调用javascript_Javascript_Python_Flask - Fatal编程技术网

从html按钮调用javascript

从html按钮调用javascript,javascript,python,flask,Javascript,Python,Flask,快速总结我正在尝试做的事情。我正在尝试使用flask制作一个从python服务器接收列表的网页。然后,它使用复选框表单添加列表中的每个元素。在使用javascript之后,我将确定选中了哪些复选框,并将字符串返回到flask服务器。我已经尝试过实现它,到目前为止,复选框列表创建工作正常。我就是不知道如何从html按钮调用javascript。我尝试使用onClick=“function”,但似乎不起作用 {% extends "layout.html" %} {% block content %

快速总结我正在尝试做的事情。我正在尝试使用flask制作一个从python服务器接收列表的网页。然后,它使用复选框表单添加列表中的每个元素。在使用javascript之后,我将确定选中了哪些复选框,并将字符串返回到flask服务器。我已经尝试过实现它,到目前为止,复选框列表创建工作正常。我就是不知道如何从html按钮调用javascript。我尝试使用onClick=“function”,但似乎不起作用

{% extends "layout.html" %}
{% block content %}

<!-- create the checkbutton lists -->
  <tr><th>Courses</th></tr>
 <br>
{% for i in class_name %}
  <tr><td>{{i.name.tagline}}</td><td><input       type="checkbox" name="ischecked"></td></tr> 
<br><br>
  {% endfor %}

<!-- checkbox -->
<script type = "text/javascript">
document.getElementById("button").click(getChecks());

// function that finds checked boxes
function getChecks()
{  
    var boxes = document.getElementsByName('ischecked');
    var checkedBox = [];
    // loop through all checkboxes  
    for(var i =0; i < boxes.length; i++)
    {
        if(boxes[i].checked)
        {
            checkedBox.push(boxes[i]);
        }        
        document.getElementById("demo").innerHTML = " " + boxes[i] + " ";
    }
    //  return checkedBox.length > 0 ? checkedBox : null
}
</script>

<div p="demo"></p>
<br>
<input type="button" id ="button" value = "advise" />

<tr><th>endpage</th></tr>
{% endblock %}`
{%extends“layout.html”%}
{%block content%}
课程

{%i在类_name%} {{i.name.tagline}

{%endfor%} document.getElementById(“按钮”)。单击(getChecks()); //查找复选框的函数 函数getChecks() { var-box=document.getElementsByName('ischecked'); var checkedBox=[]; //循环检查所有复选框 对于(变量i=0;i0?checkedBox:null }


尾页 {%endblock%}`
我首先建议将脚本放在
#按钮
按钮下方,这样首先加载DOM中的元素。下一步
onclick
在本机JavaScript中的格式如下:

obj.onclick = function(){ ... };
因此,您希望将其格式化如下:

document.getElementById("button").onclick = function() {
    getChecks();
};