用jquery绑定下拉列表
您好,我正在开发一个移动应用程序,在这个应用程序中,我试图从web服务返回的xml数据中绑定国家名称 我有以下xml。XML数据非常大,所以我在这里粘贴了一些作为示例用jquery绑定下拉列表,jquery,Jquery,您好,我正在开发一个移动应用程序,在这个应用程序中,我试图从web服务返回的xml数据中绑定国家名称 我有以下xml。XML数据非常大,所以我在这里粘贴了一些作为示例 <NewDataSet> <Table> <detail> <country country_sk="2" currency_sk="165" country_name="Afghanistan" country_telecom_code="93 " country
<NewDataSet>
<Table>
<detail>
<country country_sk="2" currency_sk="165" country_name="Afghanistan" country_telecom_code="93 " country_code="AF" />
<country country_sk="5" currency_sk="166" country_name="Albania" country_telecom_code="355 " country_code="AL" />
<country country_sk="62" currency_sk="167" country_name="Algeria" country_telecom_code="213 " country_code="DZ" />
</detail>
</Table>
我正在使用下面的代码访问web服务。我认为问题在于成功的作用
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://www.url.asmx/countryList",
dataType: "xml",
success: function (xml) {
//var result = $(xml).find("country[country_sk=2]");
//var item = $(this),
//itemid = item.attr('id');
// alert(data.nested.nestedString);
// alert(data.nested.nestedInt)
var select = $('#mySelect');
$(xml).find('country').each(function(){
var value = $(this).attr('country_name');
select.append("<option country_name='"+ country_name +"'>"+country_name+"</option>");
});
select.children(":first").text("--Select country--").attr("selected",true);
}
});
});
</script>
这是我的下拉列表HTML代码
嗨,我想我是通过修改代码发现问题的,现在出现了一个带有错误的弹出窗口
stating TypeError:无法读取null的属性“documentElement”
$(document).ready(function(){
$.ajax({
url: "http://www.mobwebservice/url.asmx/countryList",
type: "POST",
dataType: "xml",
data: soapMessage,
processData: false,
ContentType: "text/xml; charset=utf-8",
success: function (xml) {
//var xml = '<NewDataSet> <Table> <detail> <country country_sk="2"currency_sk="165" country_name="Afghanistan" country_telecom_code="93 " country_code="AF" /> <country country_sk="5" currency_sk="166" country_name="Albania" country_telecom_code="355 " country_code="AL" /> <country country_sk="62" currency_sk="167" country_name="Algeria" country_telecom_code="213 " country_code="DZ" /> </detail> </Table></NewDataSet>';
var select = $('#mySelect');
$(xml).find('country').each(function(){
var value = $(this).attr('country_name');
select.append("<option value='"+ value +"'>"+value+"</option>"); //SHould be value
});
select.val("-1"); //<-- Just use val
},
error:
function (XMLHttpRequest, textStatus, errorThrown) {
alert('Fail');
alert(errorThrown);
}
});
});
我添加了错误函数。
所以我得到了一个弹出的
TypeError:无法读取null的属性“documentElement”
请帮助我解决此错误您的值是以de变量命名的值,而不是国家/地区名称
select.append("<option value='"+ value +"'>"+value+"</option>");
你的问题是?@hjpotter92我想用xml绑定国家/地区名称,以便删除需要更正的内容。你能给我一个正确的说法吗?嗨@rst我想我无法访问web服务。如果我缺少访问web服务的任何东西,请告诉我。另外,我想知道是否有方法分配所有xml数据,这里我只给出了3个国家,作为从web服务返回给xml变量的示例。
select.append("<option value='"+ value +"'>"+value+"</option>");