<html> <head> <script type="text/javascript"src="/scripts/jquery-1.4.2.min.js"></script> </head> <body> $.ajax({ url: 'dept.xml',dataType: 'xml',success: function(data){ //console.log(data); $(data).find("dept").each(function() { var dept=$(this); var id=dept.find("id").text(); var name=dept.find("name").text(); $("#byDept").append('<optionvalue='+id+'>'+name+'</option>'); }); } }); <select id="byDept"name="byDept"> <optionvalue="0">请选择</option> </select> <body> </html> dept.xml文件如下: <?xml version="1.0"encoding="UTF-8"?> <depts> <dept> <id>1</id> <name>a</name> </dept> <dept> <id>2</id> <name>b</name> </dept> <dept> <id>3</id> <name>c</name> </dept> <dept> <id>4</id> <name>d</name> </dept> <dept> <id>5</id> <name>e</name> </dept> <dept> <id>6</id> <name>f</name> </dept> </depts>
原文链接:https://www.f2er.com/xml/299331.html