【发布时间】:2020-03-08 19:25:26
【问题描述】:
<form id="form" name="form">
<input type="checkbox" value="1" name="Asthma" data-form-field="Option" class="form-check-input display-7" id="checkbox1" title="Check if Student have Asthma">
<input type="checkbox" value="1" name="Congenital" data-form-field="Option" class="form-check-input display-7" id="checkbox1" title="Check if Student have Congenital Anomalies">
<input type="checkbox" value="1" name="Contact" data-form-field="Option" class="form-check-input display-7" id="checkbox1" title="Check if Student use Contact lens">
</form>
我的 html 中有这段代码
<script type="text/javascript">
// when page is ready
$(document).ready(function() {
// on form submit
$("#form").on('submit', function() {
// to each unchecked checkbox
$(this + 'input[type=checkbox]:not(:checked)').each(function () {
// set value 0 and check it
$(this).attr('checked', true).val(0);
});
})
})
</script>
我的问题是每次我将结果保存到我的数据库中时,结果总是自动是,即使我未选中 html 中的复选框(否)。 我不知道我的 javascript 是否正确
这是我的观点.py
Asthma = request.POST['Asthma']
Congenital = request.POST['Congenital']
Contact = request.POST['Contact']
V_insert_data = StudentUserMedicalRecord(
Asthma=Asthma,
CongenitalAnomalies=Congenital,
ContactLenses=Contact
)
V_insert_data.save()
models.py
Asthma=models.BooleanField(null=True, blank=True, default=False)
CongenitalAnomalies=models.BooleanField(null=True,blank=True, default=False)
ContactLenses=models.BooleanField(null=True,blank=True, default=False)
即使我从我的 html 插入记录也未选中(否),结果总是在我的数据库中自动“是”,你能修复我的 javascript 代码吗?好像不行。
【问题讨论】:
-
你能澄清你的问题吗?你想做什么?
-
我的意思是每次我保存到我的数据库中,即使我没有选中 html 中的复选框,结果也总是被选中
-
那是你的代码还是别人的? cmets 给你一个线索:这里有一个循环检查所有复选框:
$(this).attr('checked', true).val(0); -
我从网上复制了这段 javascript 代码,但我看不懂,我的 javascript 不太好
-
我给你发了答案。
标签: javascript django