javascript实现checkbox提交submit请求
javascript实现checkbox提交submit请求
背景:使用django模板中for来形成多个checkbox,需要点击单个checkbox并查询数据触发submit请求到后台,django收到请求后显示数据
前端代码(django模板):
<form name="sform" action="" method="GET" id="sform">
<div>
<input class="TableDivput" id="radiobutton" type="checkbox" name="radiobutton" onclick="subform()">
<input class="TableDivput" id="s_id" type="hidden" name="s_id" value="{{scenario.scenario_id}}">
<input class="TableDivput" id="s_name" name="s_name" type="text" list="typelist" value="{{scenario.scenario_name}}">
<input class="TableDivput" id="s_time" type="text" value="{{scenario.add_time}}">
</div>
</form>
Script的代码:
<script>
function subform() {
var a = document.getElementsByName("radiobutton")
var b = document.getElementsByName("sform")
for( var i=0; i<a.length; i++ ){
if (a[i].checked) {
b[i].submit()
}}
}
</script>