<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()
模型.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.py
即使我从我的 html 插入记录未被选中(否),结果在我的数据库中总是自动“是”,你能修复我的 javascript 代码吗?似乎它不起作用。