Kôd:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<title>Using jQuery and XML to populate a drop-down box demo</title>
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "dropdown.xml",
dataType: "xml",
success: function(xml) {
var select = $('#mySelect');
$(xml).find('menuitem').each(function(){
value = $(this).find('value').text();
tw=$(this).find('text').text();
select.append("<option class='ddindent' value='"+ tw +"'>"+value+"</option>");
$("#mySelect").change(function()
{
//alert(tw);
$("#TextToWrite").text($(this).val()) // use this to display value
//$("#TextToWrite").text($(this).text()) // use this to display <option> value </option>
})
select.children(":first").text("please make a selection").attr("selected",true);
});
}
});
});
</script>
</head>
<body>
<form>
<select id="mySelect">
<option>loading</option>
</select>
</form>
<div id="TextToWrite"></div>
</body>
</html>