<form id="form" name="form">
<input type="checkbox" value="1" name="C" data-form-field="Option" class="form-check-input display-7" id="checkbox1">
<input type="checkbox" value="1" name="python" data-form-field="Option" class="form-check-input display-7" id="checkbox2">
<input type="checkbox" value="1" name="Csharp" data-form-field="Option" class="form-check-input display-7" id="checkbox3" >
</form>
<script>
$(document).ready(function() {
$("#submit").on('click', function() {
console.clear()
$('input[type=checkbox]').each(function () {
if ($(this).prop('checked')==true)
console.log($(this).prop('checked'));
else
console.log($(this).prop('checked'));
});
})
})
</script>
如果用户选中数据库中必须为“是”,或者如果未选中复选框则数据库中必须为“否”,如何保存到数据库中?我的问题是,每当我取消选中这三个文件并将其保存到数据库中时,数据库中的结果将始终被自动检查,我不知道我是否在JavaScript中做得正确
这是我的views.py
Clang = request.POST["C"]
python = request.POST["python"]
Csharp = request.POST["Csharp"]
V_insert_data = known_Language(
Clang =Clang ,
python =python ,
Csharp =Csharp
)
V_insert_data.save()
这是我的模型。py
class known_Language(models.Model):
Clang = models.BooleanField(null=True, blank=True)
python = models.BooleanField(null=True, blank=True)
Csharp=models.BooleanField(null=True, blank=True)
答案 0 :(得分:2)
您不能拥有duplicate id's
。进行更改,并且default
的复选框也为checked
,因此,如果您希望在初次加载时value to 0
,可以更改unchecked
。
<form id="form" name="form">
<input type="checkbox" value="1" name="C" data-form-field="Option" class="form-check-input display-7" id="checkbox1">
<input type="checkbox" value="1" name="python" data-form-field="Option" class="form-check-input display-7" id="checkbox2">
<input type="checkbox" value="1" name="Csharp" data-form-field="Option" class="form-check-input display-7" id="checkbox3" >
</form>
答案 1 :(得分:0)
您不需要javascript,
<form id="form" name="form">
<input type="checkbox" value="1" name="C" data-form-field="Option" class="form-check-input display-7" id="checkbox1">
<input type="checkbox" value="1" name="python" data-form-field="Option" class="form-check-input display-7" id="checkbox2">
<input type="checkbox" value="1" name="Csharp" data-form-field="Option" class="form-check-input display-7" id="checkbox3" >
</form>
并将其添加到您的视图中。以下是示例:
Asthma = request.POST.get('Asthma', 1) == '0'
Congenital = request.POST.get('Congenital', 1) == '0'
Contact = request.POST.get('Contact', 1) == '0'