简体   繁体   中英

Enable textbox when checkbox ticked - php

I am trying to enable the textbox when checkbox is checked and my coding do like what I want. When submit button clicked it will check the entire form and give error information for every empty field that not been filled and keep the rest information remain to every field that been filled. The problem is, when the checkbox is checked, textbox enabled and been filled, after pressing the submit button (when all required field not completed), the textbox will become disabled with value remained.

Below is coding that I currently use :

<input type="checkbox" name="chklist" onchange="document.getElementById('txtOthers').disabled = !this.checked;" <?php if(isset($_POST['btnSubmit']) && isset($_POST['chklist'])) echo "checked" ?> /> OTHERS : 
    <input style="width: 130px;" type="textbox" id="txtOthers" name="txtOthers" disabled value="<?php echo $others;?>" />

You can use jQuery to do so :

<input type="checkbox" name="chklist" id="checkbox" <?php if(isset($_POST['btnSubmit']) && isset($_POST['chklist'])) echo "checked" ?> /> OTHERS : 
<input style="width: 130px;" type="textbox" id="txtOthers" name="txtOthers" disabled value="<?php echo $others;?>" />

$(document).ready(function(){
    $("#checkbox").change(function(){
        if ($('input.checkbox_check').is(':checked')) {
           $(#txtOthers).show();
        }else{
           $(#txtOthers).hide();
        }
    });
    $("form").submit(function() {
        $("#txtOthers").prop('disabled', true);
     });
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM