简体   繁体   English

使用jQuery从XML中填充选择框-级联选择框

[英]Populate Select box from XML with jQuery - Cascading Select boxes

I'm having a little trouble in getting this one figured out. 我在弄清楚这个问题上遇到了一些麻烦。 I need to have two SELECT boxes, in which the first select box determines the content of the 2nd one, and if one of the selects in the 2nd one is chosen, will open a link in a new window (or new tab): 我需要有两个SELECT框,其中第一个选择框确定第二个选择框的内容,如果选择了第二个选择框,则将在新窗口(或新选项卡)中打开链接:

My XML is as follows: 我的XML如下:

<?xml version="1.0" encoding="utf-8"?>
<countries>
  <country label="Australia">
    <store link="http://Link1a.com">Retailer 1a</store>
    <store link="http://Link1b.com">Retailer 1b</store>
    <store link="http://Link1c.com">Retailer 1c</store>
    <store link="http://Link1d.com">Retailer 1d</store>
    <store link="http://Link2a.com">Retailer 2a</store>
    <store link="http://Link2b.com">Retailer 2b</store>
    <store link="http://Link2c.com">Retailer 2c</store>
    <store link="http://Link2d.com">Retailer 2d</store>
  </country>
  <country label="Argentina">
    <store link="http://Link3a.com">Retailer 3a</store>
    <store link="http://Link3b.com">Retailer 3b</store>
    <store link="http://Link3c.com">Retailer 3c</store>
    <store link="http://Link3d.com">Retailer 3d</store>
    <store link="http://Link4a.com">Retailer 4a</store>
    <store link="http://Link4b.com">Retailer 4b</store>
    <store link="http://Link4c.com">Retailer 4c</store>
    <store link="http://Link4d.com">Retailer 4d</store>
  </country>
</countries>

My script is as follows: 我的脚本如下:

<script> 

$(document).ready(function() {
    var vendor_data; 
    $.get('links.xml', function(data) { 
        vendor_data = data; 
        var that = $('#countries'); 
        $('country', vendor_data).each(function() { 
            $('<option>').text($(this).attr('label')).appendTo(that);
        });
    }, 'xml');

    $('#countries').change(function() { 
        var val = $(this).val(); 
        var that = $('#store').empty(); 
        $('country', vendor_data).filter(function() { 
            return val == $(this).attr('label'); 
        }).find('store').each(function() {
            $('<option>').text($(this).text()).appendTo(that);  
        });
    });
});
</script>

HTML: HTML:

<form>
<select id="countries">
    <option value='0'>----------</option>
</select>
select id='store'>
    <option value='0'>----------</option>
</select>
</form>

Currently, I can populate the 2nd Select box with the data however, I don't know how to turn it into a "link" so that when selected, it'll open a new window with the link to the page I want visitors to go to. 目前,我可以用数据填充“第二选择”框,但是,我不知道如何将其转换为“链接”,因此,在选中该框后,它将打开一个新窗口,其中包含指向我希望访问者访问的页面的链接去。

Anyone can offer some help or advice? 任何人都可以提供帮助或建议吗?

Edit: meant to say, change the "link" in the XML to a "value" for the <option> tag eg. 编辑:就是说,将XML中的“链接”更改为<option>标签的“值”。 <option value="linkfromxml"> Retailer 2d </option>

And get the select box to open the link in a new window. 并获得选择框以在新窗口中打开链接。

A select box can't display options with links, that would be invalid HTML. 选择框无法显示带有链接的选项,该链接将是无效的HTML。 The browser wouldn't know what to do with the markup. 浏览器不知道如何处理标记。 If you're looking for this kind of behavior, you'll have to write Javascript to change the document.location based on user interaction with the select box. 如果您正在寻找这种行为,则必须编写Javascript来基于用户与选择框的交互来更改document.location

Eg: 例如:

$('select').change(function (ev) {
    var val = $(this).val();
    document.location = '/?value=' + val;
});

Why not just add a change listener to the second select? 为什么不只将更改侦听器添加到第二个选择项? Something like. 就像是。

$('#store').change(function() { 
   document.location = $(this).val();
});

Update: 更新:

Demo here 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM