这是我第一次使用jQuery
,我需要你的帮助,因为我没有克服做我想要的。
我有一个文档表,每个文档都有一个复选框,以便选择或不选择它。
我还有一个字段,其中一个用户设置了新的一年,以便在之后修改文件名。
我有这个过程:
这是我的 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 步中运行良好,但如果取消选中该复选框则不行。
你能帮我解释一下我的代码中有什么错误吗?
HTML5 验证可以为您做到这一点。不需要 JavaScript。只需将required
属性添加到元素。
<form>
<label>
<label for="name">name</label><input id="name" required />
<br/>
<input type="checkbox" required name="terms"> I accept the Terms and Conditions
</label>
<br/>
<input type="submit">
</form>
但是如果你想用 JavaScript 来做,那么你可以创建一个方法来做这两项检查。用一种方法完成所有检查比试图弄清楚如何对每个输入进行单独检查更容易。
function checkValid () {
var cbChecked = $("#cb").is(":checked"); // check if checked
var hasText = $("#name").val().length > 0; // check if it has text
$("#btnSubmit").prop("disabled", !cbChecked || !hasText);
}
$( function () {
checkValid(); // run it for the first time
$("#cb").on("change", checkValid); // bind checkbox
$("#name").on("input", checkValid) // bind textbox
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<label>
<label for="name">name</label><input id="name" />
<br/>
<input id="cb" type="checkbox" name="terms"> I accept the Terms and Conditions
</label>
<br/>
<input type="submit" id="btnSubmit">
</form>
如果您有多个复选框并且只需要一个复选框,那么您需要使用 JavaScript
function checkValid () {
var cbChecked = $(".cb:checked").length > 0; // check if at least one checked
var hasText = $("#name").val().length > 0; // check if it has text
$("#btnSubmit").prop("disabled", !cbChecked || !hasText);
}
$( function () {
checkValid(); // run it for the first time
$(".cb").on("change", checkValid); // bind checkboxesvia classname
$("#name").on("input", checkValid) // bind textbox
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<label>
<label for="name">name</label><input id="name" />
<br/>
<label>
<input class="cb" type="checkbox" name="terms"> One
</label>
<label>
<input class="cb" type="checkbox" name="terms"> Two
</label>
<label>
<input class="cb" type="checkbox" name="terms"> Three
</label>
<br/>
<input type="submit" id="btnSubmit">
</form>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句