使用jquery链接下拉列表:不保留值

时间:2016-03-22 19:27:14

标签: php jquery html

我在代码中可以看到两个链接的下拉列表。

 <?php

    $subdept=$dept="";
    $subdeptError=$deptError="";
    if(isset($_POST['submit'])){
      $subdept=$_POST['subdept'];
      $dept=$_POST['dept'];

     echo "dept is".$dept."<br>";
     echo "subdept is".$subdept."<br";


      }




    echo <<<_END
    <html>
    <head>
    <title>Testing</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

        <script type="text/javascript" src="js/jquery.1.11.1.js"></script>
     <script>
            $(function() {

                $("#dept").change(function() {

                                  $("#subdept").load("textdata/" + $(this).val() + ".txt");

                });


            });
        </script>
    </head>
    <body>
    <form action="testsubdept.php" method="post">
    <div class="input-group col-xs-8">
    <select class="form-control" id="dept" name="dept">
    _END;
    ?>                         



                        <OPTION value="z" <?php if (!isset($_POST['dept']) OR ($_POST['dept']=="z") ) {echo "selected='selected'"; } ?> >Please select option</OPTION>
                        <OPTION value="a" <?php if(isset($_POST['dept'])) if ($_POST['dept']=="a") {echo "selected='selected'"; } ?> >Exploration</OPTION>
                        <OPTION value="b" <?php if(isset($_POST['dept'])){ if ($_POST['dept']=="b") {echo "selected='selected'"; }} ?> >Operations</OPTION>

                        <?php
                        echo <<<_END
                            </select>


                            <select class="form-control" id="subdept" name="subdept">


                                <OPTION value="0">Please select option</OPTION>




                            </select>
                         <input type="submit" name="submit" id="submit" value="Submit" >   
                        </div>
    </form>

    </body>
    </html>

    _END;

    ?>

和z.txt的文本为

<option value="0">Please choose from above</option>

作为

的a.txt文本
<OPTION value="1" <?php if(isset($_POST['subdept'])) if ($_POST['subdept']==1) echo "selected='selected'"; } ?>Chemistry</OPTION>
<OPTION value="2" <?php if(isset($_POST['subdept'])){ if ($_POST['subdept']==2) {echo "selected='selected'"; }} ?>GeoPhysics(Wells)</OPTION>
<OPTION value="3" <?php if(isset($_POST['subdept'])){ if ($_POST['subdept']==3) {echo "selected='selected'"; }} ?>Geology</OPTION>

b.txt的文字为

<option value="14" <?php if(isset($_POST['subdept'])){ if($_POST['subdept']==14) {echo "selected='selected'"; }} ?>Production</option>
<option value="15" <?php if(isset($_POST['subdept'])){ if($_POST['subdept']==15) {echo "selected='selected'"; }} ?>Marketing</option>
<option value="16" <?php if(isset($_POST['subdept'])){ if($_POST['subdept']==16) {echo "selected='selected'"; }} ?>Enviromental Engineer</option>

问题是:为什么我无法在表单提交中保留subdept的价值?以及如何做到这一点?

0 个答案:

没有答案