我有一个级联选择并运行。我想要的是,当我点击一个国家时,只会显示其相关区域。 现在一直有效,直到我提交表格。提交表单后,新创建的区域列表中的值将设置为填充列表之前设置的默认值。
我希望它的工作方式类似于这种形式的列表:Link To Example of choice being the default of a dropdown list如果您点击链接1,您会看到“Carlow'”的位置。已设定。当您单击该下拉列表时,它的起点是Carlow。我尝试了两个jquery函数。 1.填充第二个列表,选择第一个和2.尝试发送第二个选择以及尝试将其设置为默认值的国家。
我目前有一个getoptions.php文件:
$sql = "SELECT area FROM Country ORDER BY county ASC";
$result = mysqli_query($conn,$sql);
while($state = mysqli_fetch_assoc($result)){
$area =$state['area'];
//get the location if it is set, if not set let it be null
$location = isset($_GET['location']) ? $_GET['location'] : NULL;
$selectedString = "";
if($location && ($area == $location)){
$selectedString = "selected";
}
echo "<option $selectedString value='$area'>$area</option>";
}
主页:
<select name="location" id="location">
<option>Please choose a location</option>
</select>
<script>
$("#country").on('change', function() {
var country = $(this).val();
$("#location").load("/getoptions.php?country="+country);
});
$("#submit").on('click', function() {
var country = $('#country').val();
var location = $('#location').val();
$("#location").load("/getoptions.php?country="+country+"&location="+location);
});
答案 0 :(得分:0)
这有点帮助吗?它应该回退所有区域,如果设置了位置参数,它应该选择该位置。
//I'm assuming your table is set up something like this
//id | country | area
//This code checks if there's a country parameter set and if there is
//it uses gets all the areas for that country
$country = (isset($_GET['country']) ? $_GET['country'] : NULL;
$sql = "SELECT area FROM Country ";
if($country){
$sql.= " WHERE Country.country = '$country' "
}
$sql.= "ORDER BY county ASC";
$result = mysqli_query($conn,$sql);
while($state = mysqli_fetch_assoc($result)){
$area = $state['area'];
//get the location if it is set, if not set let it be null
$location = (isset($_GET['location']) ? $_GET['location'] : NULL;
$selectedString = "";
if($location && ($area == $location)){
$selectedString = "selected";
}
echo "<option $selectedString value='$area'>$area</option>";
}
如果您使用网址<:p>转到浏览器中的上述文件
www.site.com/getoptions.php?country=Ireland&location=Dublin
它应该输出如下内容:
<option value="Antrim">Antrim</option>
<option value="Armagh">Armagh</option>
<option value="Dublin" selected>Dublin</option>
如果你得到上述结果,你就会走上正轨。如果没有,那么php会出错。
在您的主页上尝试以下内容:
<select id="country">
<option value="Ireland">Ireland</option>
</select>
<select id="location">
</select>
<script>
$(document).ready(function(){
$("#country").on('change', function() {
var country = $(this).val();
$("#location").load("/getoptions.php?country="+country);
});
$("#submit").on('click', function() {
var country = $('#country').val();
var location = $('#location ').val();
$("#location").load("/getoptions.php?country="+country+"&location="+location);
});
}); //doc ready
看看是否能让你走上正轨。