在提交按钮

时间:2017-08-18 10:05:46

标签: javascript php html

在此代码中,当我选择选项3值时,我的业务div将可见(对于选项1和2值,它将被隐藏)但在提交按钮后业务div不可见。所以,请帮我解决它。

HTML代码

    <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"

<table width="30%" border="1px solid white"><tr border="1px solid white">
<td><select id='purpose' name="purpose" style="color:black">

            <option value="1" <?php echo $d1 ?>>Current Day</option>
            <option value="2" <?php echo $d2 ?>>Current Month</option>
            <option value="3" <?php echo $d3 ?>>Custom</option>
    </select></td></tr></table></br>
    <div style='display:none;' id='business'>

    <table width="50%"><tr> <td width="50%">Start Date<input style="color:black;background-color:white;" type="text" id="basic_example_1" name="stdt" placeholder="YYYY-MM-DD HH:MM"/>


            </td>
    <td >End Date<input style="color:black;background-color:white;" type="text" id="basic_example_3" name="spdt" placeholder="YYYY-MM-DD HH:MM"/>

            </td></tr></table>
    </div>

    <button type="submit" name="table"  value="reg" class="btn btn-success" style="width:20%;height:35px;margin-top:5px" >Submit</button>

        </form>

Javascript代码

<script>

$(document).ready(function(){

      $('#purpose').on('change', function() {
      if ( this.value == '3')
      {
        $("#business").show();


      }
      else if(this.value == '2')
      {
        $("#business").hide();
      }
      else if(this.value == '1')
      {
                  $("#business").hide();

      }
    });


});


   </script>    

2 个答案:

答案 0 :(得分:5)

试试这段代码。

 <?php
        $DispDiv ="display:none;";

        if(isset($_GET['purpose']) && ($_GET['purpose'] == 3)){
            $DispDiv ="display:block;";         
        }

    ?>

        <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" <table width="30%" border="1px solid white">
            <tr border="1px solid white">
                <td>
                    <select id='purpose' name="purpose" style="color:black">

                        <option value="1" <?php echo $d1 ?>>Current Day</option>
                        <option value="2" <?php echo $d2 ?>>Current Month</option>
                        <option value="3" <?php echo $d3 ?>>Custom</option>
                    </select>
                </td>
            </tr>
            </table>
            </br>

            <div style='<?php echo $DispDiv;?>' id='business'>

                <table width="50%">
                    <tr>
                        <td width="50%">Start Date
                            <input style="color:black;background-color:white;" type="text" id="basic_example_1" name="stdt" placeholder="YYYY-MM-DD HH:MM" />

                        </td>
                        <td>End Date
                            <input style="color:black;background-color:white;" type="text" id="basic_example_3" name="spdt" placeholder="YYYY-MM-DD HH:MM" />

                        </td>
                    </tr>
                </table>
            </div>

            <button type="submit" name="table" value="reg" class="btn btn-success" style="width:20%;height:35px;margin-top:5px">Submit</button>

        </form>
        <script>
            jQuery(document).ready(function($) {

                $('#purpose').on('change', function() {
                    if (this.value == '3') {
                        $("#business").show();

                    } else if (this.value == '2') {
                        $("#business").hide();
                    } else if (this.value == '1') {
                        $("#business").hide();

                    }
                });

            });
        </script>

答案 1 :(得分:-2)

提交表单后,change #purpose事件将被触发,根据您编写的脚本,它将被隐藏。