【发布时间】:2018-10-29 17:54:37
【问题描述】:
我正在处理我的Django 项目,我遇到了Javascript 部分和IE 11 的问题。
我的函数在 Firefox 和 Chrome 上运行良好,但在 Internet Explorer 11 上却不如我所愿。
流程:
我有一个带有复选框、下拉列表和提交按钮的表格。 当我没有选中任何复选框并在下拉列表中选择任何值时,此按钮被禁用。
图片:
如您所见,提交按钮在以下情况下被禁用:
- 未选中任何复选框和列表值
- 复选框已选中,但未列出值
- 复选框未选中,但列表值已选中
仅在选择复选框和列表值时才启用提交按钮。
我的代码:
Javascript 部分:
<script type="application/javascript">
function checkValid() {
var cbChecked = $(".fake-radio").is(":checked"); // check if checked
var selectelem = document.getElementById('manage-doc-year');
var btnelem = document.getElementById('document-button');
btnelem.disabled = !selectelem.value;
var dropdown_value = btnelem.disabled;
$("#document-button").prop("disabled", !cbChecked || dropdown_value);
}
$(function () {
checkValid(); // run it for the first time
$(".fake-radio").on("change", checkValid); // bind checkbox
$("#manage-doc-year").on("input", checkValid) // bind textbox
});
</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="document-table" class="table table-bordered table-striped table-condensed table_model">
<thead>
<tr>
<th>{% trans 'Choice' %}</th>
<th>{% trans 'Document title' %}</th>
</tr>
</thead>
<tbody>
{% for document in query_document %}
<tr>
<td><input type="checkbox" class="fake-radio" id="document-checkbox" name="DocumentChoice"
value="{{ document.id }}"></td>
<td>{{ document.title }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<select id="manage-doc-year" name="q1year" value="{{ request.get.q1year }}" required>
<option selected="selected">
<option value="" selected disabled hidden></option>
</option>
</select>
<button class="btn btn-default" id="document-button" type="submit"
name="UpdateDocument">{% trans "Submit" %}</button>
</form>
</div>
IE 11:
使用 IE 11,它只有在我先选择下拉值,然后选中复选框时才有效。但不是如果我先签到,然后我选择值。为什么?
谢谢!
【问题讨论】:
标签: javascript jquery html internet-explorer internet-explorer-11