【发布时间】:2018-08-22 13:59:49
【问题描述】:
这是我第一次使用jQuery,我需要你的帮助,因为我没有克服自己想要的东西。
我有一个文档表,每个文档都有一个复选框,以便选择或不选择它。
我还有一个字段,其中一个用户设置了一个新年以便在之后修改文件名。
我有这个过程:
- 默认情况下,提交按钮是禁用的
- 要激活提交按钮,用户必须选中一个复选框并且写新年
- 完成第 2 步后,如果我删除年份或取消选中该复选框,则必须禁用提交按钮
这是我的 javascript 部分(别忘了,这是我的第一次):
<script type="application/javascript">
$(document).ready(function(){
$('#DocumentButton').attr('disabled',true);
$('#year').keyup(function(){
// if field year is not empty and checkbox is checked
if($(this).val().length !=0 && $('.fakeRadio').is(':checked'))
$('#DocumentButton').attr('disabled', false);
// if field year is empty or checkbox isn't checked
else if ($(this).val().length =0 || $('.fakeRadio').is(':checked')==false)
$('#DocumentButton').attr('disabled',true);
})
});
// Simulate Checkbox as Radiobox
$(document).ready(function(){
$(".fakeRadio").click(function(){
var wasChecked = !$(this).prop("checked");
$(".fakeRadio").prop( "checked", false );
$(this).prop("checked", (!wasChecked) ? true : false );
});
});
</script>
以及相应的 HTML 部分:
<div class="col-md-offset-1 col-md-5">
<h4> {% trans "List of documents:" %} </h4>
<form action="" method="POST"> {% csrf_token %}
<table id="DocumentTable" class="table table-bordered table-striped table-condensed table_model"
style="width: 160%;">
<thead>
<tr>
<th style="width: 10%;">Choice</th>
<th>Document title</th>
</tr>
</thead>
<tbody>
{% for document in query_document %}
<tr>
<td><input type="checkbox" class="fakeRadio" id="DocumentCheckBox" name="DocumentChoice" value="{{ document.id }}"></td>
<td>{{ document.title }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<input type="text" id="year" name="q1year" placeholder="Set new year" value="{{ request.get.q1year }}">
<button class="btn btn-default" id="DocumentButton" type="submit" name="UpdateDocument">{% trans "Submit" %}</button>
</form>
</div>
到目前为止,该过程在第 1 步和第 2 步中运行良好,但如果我取消选中该复选框,则不会。
你能帮我解释一下我的代码中有什么错误吗?
【问题讨论】:
标签: javascript jquery