选中复选框时需要显示文本框

时间:2018-03-28 12:02:19

标签: jquery css

<input type="checkbox" id="permit">
<input type="text" class="form-control" placeholder="Enter Short Code" name="short_code" id="short_code" autocomplete="off" style="display: none;">

使用jQuery

选中复选框时,如何显示文本框

3 个答案:

答案 0 :(得分:2)

每当您点击复选框时,您都可以检查:checked属性:

$(document).ready(function(){
  $('#permit').click(function(){
    $(this).is(':checked')? $('#short_code').show(): $('#short_code').hide();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="permit">
<input type="text" class="form-control" placeholder="Enter Short Code" name="short_code" id="short_code" autocomplete="off" style="display: none;">

答案 1 :(得分:0)

您可以添加简单的CSS规则。

#permit:not(:checked) + #short_code {
    display: none;
}

答案 2 :(得分:0)

解决方案:

    <input type="checkbox" id="permit">
<input type="text" class="form-control" placeholder="Enter Short Code" name="short_code" id="short_code" autocomplete="off" style="display: none;">

&#13;
&#13;
$(function () {
       //show it when the checkbox is clicked
        $('#permit').on('click', function () {
            if ($(this).prop('checked')) {
                $('#short_code').fadeIn();
            } else {
                $('#short_code').hide();
            }
        });
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="permit">
    <input type="text" class="form-control" placeholder="Enter Short Code" name="short_code" id="short_code" autocomplete="off" style="display: none;">
&#13;
&#13;
&#13;