I have a MVC application in C# and in a view that will have some checkboxes and on this list of checkboxes some checkboxes will come "checked and disable" from the database depends if I pass some or other value of ID.
But my problem is my button only can submit if i check a checkbox enable or if i have more than 2 enable and checked all the checkboxes enabled and not checked the checkbox disabled. click here
<input type="checkbox" name="SelectAll" id="checkboxPrincipal" />SelectALL
@foreach (var item in Model)
{
<input type="checkbox" id="mycheckbox" class="mycheck" onchange="isChecked(this,'myButton');" checked="checked" disabled="disabled"/>
<input type="checkbox" id="mycheckbox" class="mycheck" onchange="isChecked(this,'myButton');"/>
<input type="checkbox" id="mycheckbox" class="mycheck" onchange="isChecked(this,'myButton');"/>
}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" id="myButton" name="myButton" disabled >Actualizar</button>
<script type="text/javascript">
function isChecked(mycheckbox,myButton) {
console.log(myButton);
var myLayer = document.getElementById(myButton);
if (mycheckbox.checked == true) {
myLayer.disabled = false;
} else {
myLayer.disabled = true;
}
}
$('.checkboxPrincipal').change(function () {
if ($('.checkboxPrincipal:checked').length == $('.checkboxPrincipal').length) {
$('#myButton').prop('disabled',true);
}
else{
$('#myButton').prop('disabled',false);
}
});
</script>
you can use checked attribute of checkbox and disabled attribute of button in jQuery like this example :
$(document).ready(function () {
if(chkbox1.checked==true) {
$('button').attr('disabled','disabled');
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.