如何在选中复选框时禁用或启用Bootstrap提交按钮

3
我需要在表单上启用或禁用Bootstrap提交按钮,根据复选框是否被选中。
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'>";
3个回答

18

对于复选框,你只需要获取提交按钮的Id(在这种情况下为grabaperaus),在checkboxonChange事件中禁用它即可。

<input type="checkbox"  
onchange="document.getElementById('grabaperaus').disabled = !this.checked;" name='visto' 
id='visto'/>

3

使用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);});

1
尝试这个。

 
$(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'>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接