如何根据第一个select
的值添加另一个select
?如何在php中进行链操作
当第一select
箱变,我想打一个AJAX请求到另一个select
,基于第一select
的值取值,并把它们插入作为DOM在新select
选项。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Populate City Dropdown Using jQuery Ajax</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.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">
</td>
</tr>
</table>
</form>
</body>
</html>
我只能看到1选择在你的HTML,但使用jQuery,你可以简单地使用'$( “#选择2”)。VAL($( “#选择1”)。VAL( ));'如果你想将第二个select设置为第一个的值。 – xander
'process-request.php'返回什么? 如果你运行这段代码,选择一个国家后''td id =“response”>'是什么内容? –