3

I need to enable or disable a bootstrap submit button on a form if a checkbox is cheched or not

echo "<div class='panel panel-danger'><div class='panel-heading'>OBSERVACIONES</div><div class='panel-body'>";
echo "<div class='input-group'>";
echo "<label class='checkbox-inline'><input type='checkbox' name='visto' id='visto' value=''> Visto bueno del Responsable</label>";
echo "</div>"; 
echo "</div>"; 

...

echo "<input type='submit' class='btn btn-primary' value='SAVE' name='grabaperaus' formaction='update.php'>";
osbragon
  • 103
  • 2
  • 2
  • 8

3 Answers3

18

For the checkbox all you need to do is get the Id of your submit button (grabaperaus in this case) disable it onChange event of the checkbox.

<input type="checkbox"  
onchange="document.getElementById('grabaperaus').disabled = !this.checked;" name='visto' 
id='visto'/>
Coding Enthusiast
  • 3,865
  • 1
  • 27
  • 50
3

With Jquery
HTML:

<input type='checkbox' data-toggle='inputid'/>
<input id='inputid'/>

JAVASCRIPT:

function toggle(target) {
    var toggle = $(target).data("toggle");
    if (toggle) {
        var obj = $("#" + toggle);
        if (target.checked ==false) {
            obj.attr("disabled", "disabled");
        } else {
            obj.removeAttr("disabled");
        }
    }
}

$("input:checkbox").each(function(){toggle(this);}).on('input',function(){toggle(this);});
Walter Verhoeven
  • 3,867
  • 27
  • 36
Sveen
  • 347
  • 2
  • 10
1

try this

 
$(document).ready(function(){
    $('#save').prop('disabled', true);

    $('#visto').click(function(){
        if($(this).is(':checked'))
        {
            $('#save').prop('disabled', false);
        }
        else
        {
            $('#save').prop('disabled', true);
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class='checkbox-inline'><input type='checkbox' name='visto' 
id='visto' value=''> Visto bueno del Responsable</label>

<input type='submit' class='btn btn-primary' id="save" value='SAVE' name='grabaperaus' formaction='update.php'>
ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
JYoThI
  • 11,977
  • 1
  • 11
  • 26