我一直在为自己的工作开发一个小型工具,但实际上并不能使最后的工作正常工作。
您可以在此处查看代码等:http : //jsfiddle.net/wb157kbx/5/
我想做的是,我希望country
从XML
到下拉菜单都一样。当你选择一个country
,它显示mobile
,phone
并code
从数据XML
中的#tal
标签。我正在用JQuery做到这一切。
我在做什么错,因为我没有得到选择price
,phone
,code
等?
$('#dropdown').change(function () {
$selected = $('#dropdown').find(":selected");
var id = $selected.prop("id");
$(xml).find('item[id="' + id + '"]').each(function ({
var code = $(this).find('code').text();
var fastnet = $(this).find('phone').text();
var mobil = $(this).find('mobile').text();
$("#pris").text("Mobil: " + mobil + " Fastnet: " + fastnet + " Landkode: " + code);
});
问题在于,您过早将值添加到了全局范围ID。这样,您的ID将始终是xml上的最后一个ID。
您需要解决所选的选项ID
var id = $selected.prop("id");
我会做的是:
success: function (xml) {
// Parse the xml file and get data
var xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc),
options = "";
$(xml).find('item').each(function () {
id = $(this).attr('id');
land = $(this).find('country').text();
code = $(this).find('code').text();
fastnet = $(this).find('phone').text();
mobil = $(this).find('mobile').text();
options += "<option data-id='"+id +"' data-land='"+land +"' data-code='"+code +"' data-fastnet='"+fastnet +"' data-mobil='"+mobil+"' >" + land + "</option>";
});
$("#dropdown").html(options)
}
然后在更改时,我将执行以下操作:
$('#dropdown').change(function () {
$selected = $('#dropdown').find(":selected");
$("#pris").text("Mobil: " +$selected.data("mobil")+
" Fastnet: " +$selected.data("fastnet") +
" Landkode: " + $selected.data("code"));
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句