Php 在下拉列表中保留选定值

Php 在下拉列表中保留选定值,php,jquery,Php,Jquery,jQuery代码:我无法在state city下拉列表中保留所选值。单击“立即注册”按钮时,城市字段变为空白。任何帮助都将不胜感激 //script for displaying city on the basis of state $(document).ready(function(){ $("#state").change(function(){ var selectedState = $("#state option:selected").

jQuery代码:我无法在state city下拉列表中保留所选值。单击“立即注册”按钮时,城市字段变为空白。任何帮助都将不胜感激

//script for displaying city on the basis of state
    $(document).ready(function(){
        $("#state").change(function(){
            var selectedState = $("#state option:selected").val();
            $.ajax({
                type: "POST",
                url: "ajax/ajax_city.php",
                data: { state : selectedState } 
            }).done(function(data){
                $("#city").html(data)
            });
        });
    });

    // script for displaying states

    $(function(){
        $.get("ajax/ajax_state.php", function(data) {
            $("#state").html(data);
        });
    });
//script for retaining selected values

        $(document).ready(function(){
        $('#state').val("<?php echo $_POST['state'];?>")
        $('#city').val("<?php echo $_POST['city'];?>");
        $('#expertise').val("<?php echo $_POST['expertise'];?>");
        $('#practice_in').val("<?php echo $_POST['practice_in'];?>");
    });
//提交按钮的html

<button md-ink-ripple="" type="submit" class="btn btn-success btn-lg btn-block" id="ad_sign_up" name="ad_sign_up">Register Now!</button>
//显示城市的php代码:ajax_city.php

 <?php
include("../includes/state-array.inc.php");
#include("./includes/state-array.php");
    $state = $_POST["state"];
    // Display city dropdown based on state name
    if($state !== 'Select'){
        foreach($indStateCity[$state] as $value){
            echo "<option>". $value . "</option>";
        }
    } 
     ?>
//显示状态的php代码:ajax_state.php

<?php
include("../includes/state-array.inc.php");
echo "<option selected='selected'>State</option>";
foreach($indStateCity as $key => $value){
            echo "<option>". $key . "</option>";
        }
?>

$'state'.val.attr已选中;显示您按下的按钮的HTML值得检查:选择列表中的name和id值是否相同?发布的是名称,但您的选择器位于id上。我猜,按钮是type=submit,您正在运行AJAX代码并提交表单。请看,您还忘了显示PHP代码,因此我们不知道从该脚本返回数据的内容或方式