$('#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"));
});
控制檯說「跨源請求被阻止:同源策略不允許讀取位於http://www.patrickrorth.dk/Arbejde/udlandspr的遠程資源iser.xml。這可以通過將資源移動到相同的域或啓用CORS來解決。「 – user733421 2014-10-22 09:31:39
這是JSFiddle還是演示? – 2014-10-22 09:32:51
是JSFiddle。也許這就是你的問題 – user733421 2014-10-22 09:33:50