我有一个问题,我有2个复选框,如果复选框的值为Y,我想显示一个文本框,我的代码:

<label>Admin:</label>
                <div class="onoffswitch">
                    <input type="checkbox" name="is_admin" class="onoffswitch-checkbox" value="Y" id="is_admin">
                    <label class="onoffswitch-label" for="is_admin">
                        <span class="onoffswitch-inner"></span>
                        <span class="onoffswitch-switch"></span>
                    </label>
                </div>
                <label>Verificator:</label>
                <div class="onoffswitch">
                    <input type="checkbox" name="is_verificator" class="onoffswitch-checkbox" value="Y" id="is_verificator">
                    <label class="onoffswitch-label" for="is_verificator">
                        <span class="onoffswitch-inner"></span>
                        <span class="onoffswitch-switch"></span>
                    </label>
                </div>
                <input type="text" style="display:none;" placeholder="" id="if_checked"/><br/>


我的jQuery:

<script>
$("input[name=is_verificator]").change(function(){

    if($(this).val() == "Y")
    {
        $("#if_checked").show();
    }else{
        $("#if_checked").hide();
    }
});

最佳答案

只需将jQuery代码包装在$(document).ready(function(){})内,如下所示:

<script>
$(document).ready(function(){
   $("input[name=is_verificator]").change(function(){
     if($(this).val() == "Y" && $(this).is(":checked"))  //changed this condition as per OP comment
     {
       $("#if_checked").show();
     }else{
       $("#if_checked").hide();
     }
  });
});
</script>

09-25 17:14
查看更多