Enabling inactive fields by clicking on field

前端 未结 1 1449
星月不相逢
星月不相逢 2021-01-29 04:46

Is it possible to have a group of inactive fields where if one of the fields is clicked some of the fields become mandatory and some segments of code are run? Say for example yo

相关标签:
1条回答
  • 2021-01-29 05:34

    This method you are looking is called 'chained-select' method.

    jQuery framework is used for example below:

    <html>
    <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    </head>
    <body>
    <form action="" method="get">
        <label>
        <input name="Government" type="checkbox" value="">Government</label>
        <br>
        <label>
        <input name="Parent" type="checkbox" id="parent_child_group" value="">Parent</label>
        <br>
        <label>Name of Child
        <input type="text" name="parent_child" value="" class="parent_child_group"></label>
        <br>
        <label>Other
        <input type="text" name="parent_other" value="" class="parent_child_group"></label>
    </form>
    
    <script type="text/javascript">
        $(document).ready(function () {
             $(function() {
                 enable_cb();
                 $("#parent_child_group").click(enable_cb);
             });
    
             function enable_cb() {
                 if (this.checked) {
                     $("input.parent_child_group").removeAttr("disabled");
                 } else {
                     $("input.parent_child_group").attr("disabled", true);
                 }
             }
        });        
        </script>
        </body>
        </html>
    

    Working example on JSFiddle: http://jsfiddle.net/farondomenic/fg7p8/1/

    0 讨论(0)
提交回复
热议问题