gpt4 book ai didi

php - 根据另一个选择显示一个选择的下拉值

转载 作者:行者123 更新时间:2023-11-29 06:04:41 26 4
gpt4 key购买 nike

我有一个“项目”表,如下所示

Project_ID   |   Client_ID
---------------------------
P1 C1
P2 C1
P3 C2
P4 C2
P5 C3
P6 C3

我有一个显示所有客户端 ID 的 select id="1"

我正在显示另一个 select id="2" 以显示分配给客户 ID 的所有项目。

基本上我想根据从 select id="1" 中选择的值显示 select id="2"option 值.

如果有人选择的例子

<option value="C3">C3</option>

那么下面应该有

<select id="2">
<option value="P5">P5</option>
<option value="P6">P6</option>
</select>

我认为 Ajax 是答案,但我不知道正确的方法。

最佳答案

您可以为选择框的 onChange 事件添加一个事件监听器。在更改事件中获取选择框的值并使用 ajax 请求将其值发送到服务器,并根据第一个选择框获取要在第二个选择框中显示的值,并将其显示在第二个选择框中。基于国家选择的州选择示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Populate City Dropdown Using jQuery Ajax</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select.country").change(function(){
var selectedCountry = $(".country option:selected").val();
$.ajax({
type: "POST",
url: "process-request.php",
data: { country : selectedCountry }
}).done(function(data){
$("#response").html(data);
});
});
});
</script>
</head>
<body>
<form>
<table>
<tr>
<td>
<label>Country:</label>
<select class="country">
<option>Select</option>
<option value="usa">United States</option>
<option value="india">India</option>
<option value="uk">United Kingdom</option>
</select>
</td>
<td id="response">
<!--Response will be inserted here-->
</td>
</tr>
</table>
</form>
</body>
</html>

后端:

<?php
if(isset($_POST["country"])){
// Capture selected country
$country = $_POST["country"];

// Define country and city array
$countryArr = array(
"usa" => array("New Yourk", "Los Angeles", "California"),
"india" => array("Mumbai", "New Delhi", "Bangalore"),
"uk" => array("London", "Manchester", "Liverpool")
);

// Display city dropdown based on country name
if($country !== 'Select'){
echo "<label>City:</label>";
echo "<select>";
foreach($countryArr[$country] as $value){
echo "<option>". $value . "</option>";
}
echo "</select>";
}
}
?>

关于php - 根据另一个选择显示一个选择的下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com