jQuery代码:我无法在州城市下拉列表中保留选定的值。当我点击立即注册按钮时,城市字段变为空白。任何帮助将不胜感激。
//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 for submit button
<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>";
}
?>