$(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
}
});
});
I have a dynamic select option, country->state, use jquery load files. However I need to auto select the option from DB. ex. if user's DB's country is usa, than the option will auto select usa.
something like this
$country=$data['country'];//query from DB
if($("#country").val()==<?PHP echo $country;?>){select the option...}
You can try this:
<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>
(..)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.