$(document).ready(function(){
$("#country").load("country.php");
$("#state").load("state.php");
$("#country").change(function(){
switch($("#country").val()){
case "aus":$("#state").load("aus_state.php");break
case "eng":$("#state").load("eng_countie.php");break
case "usa":$("#state").load("us_state.php");break
}
});
});我有一个动态选择选项,country->state,使用jquery加载文件。然而,我需要从数据库中自动选择该选项。例如。如果用户的数据库的国家是美国,那么该选项将自动选择美国。
像这样的东西
$country=$data['country'];//query from DB
if($("#country").val()==<?PHP echo $country;?>){select the option...}发布于 2013-06-09 12:23:01
您可以尝试这样做:
<html>
<head>
<title>Page</title>
</head>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
//load countries
$("#country").load("country.php");
//print php variable into js
var country = '<?php echo $data["country"]; ?>';
//set the item that matches country variable as selected
$("#country option").filter(function() { return $(this).text() == country; }).prop("selected", true);
});
</script>
<body>
(..)https://stackoverflow.com/questions/17006371
复制相似问题