【发布时间】:2021-05-26 04:31:23
【问题描述】:
我有复选框,例如,当我选中其中的 3 个然后取消选中 1 时,一个属性在对立面起作用。我的意思是因为我有一个默认隐藏按钮,当我选中一个复选框时会出现,当我选中了 3 个复选框并取消选中 1 个时,按钮被隐藏,因为仍然选中了 2 个复选框,这意味着按钮应该不隐藏。我该如何纠正这个????
<script>
$(document).ready(function() {
$('#select-all').click(function() {
var checked = this.checked;
$('input[type="checkbox"]').each(function() {
this.checked = checked;
});
})
});
//Add a JQuery click event handler onto our checkbox.
$('input[type="checkbox"]').click(function(){
//If the checkbox is checked.
if($(this).is(':checked')){
//Enable the submit button.
$('#delete_all').attr("hidden", false);
} else{
//If it is not checked, hide the button.
$('#delete_all').attr("hidden", true);
}
});
</script>
<form method="post" action="{% url 'delete_students' %}">
{% csrf_token %}
<table class="layout container">
<thead>
<th><input type="checkbox" id="select-all"></th>
</thead>
<tr>
<td>
<input type="checkbox" name="marked_delete" value="{{ student.pk }}">
</td>
</table>
<button class="btn btn-dark" id="delete_all" hidden="hidden">Delete Selected</button>
</form>
【问题讨论】: