douhui0975 2017-08-18 10:05 采纳率: 100%
浏览 99
已采纳

在提交按钮后使用javascript处理php中的div标签可见性

In this code when I select option 3 value my business div will be visible(For option 1 and 2 value it will be hidden) but after submitting button the business div not visible.so, please help me to solve it.

HTML code

    <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 code

<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条回答 默认 最新

  • dongtangu8615 2017-08-18 10:13
    关注

    Try this code.

     <?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条)

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换