It's the first time I'm using jQuery and I need your help because I don't overcome to make what I want.
I have a table of documents with a checkbox for each document in order to select it or not.
I have also a field in which one users set a new year in order to modify filename after.
I have this process :
- By default, submit button is disabled
- To activate the submit button, user have to check one checkbox AND write the new year
- Once the step 2 is done, if I remove year OR uncheck the checkbox, submit button have to be disabled
This is my javascript part (don't forget, it's my first time) :
<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>
And the according HTML part :
<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>
Up to now, the process works fine with step 1 and 2, but not if I uncheck the checkbox.
Could you help me and explain what is false in my code ?