首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jQuery填充来自XML的选择框-级联选择框

使用jQuery填充来自XML的选择框-级联选择框
EN

Stack Overflow用户
提问于 2014-02-26 09:41:32
回答 2查看 645关注 0票数 0

我在解决这个问题上遇到了一点小麻烦。我需要两个选择框,其中第一个选择框确定第二个的内容,如果第二个选择框中的一个被选中,将在新窗口(或新选项卡)中打开一个链接:

我的XML如下:

代码语言:javascript
复制
<?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>

我的脚本如下:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<form>
<select id="countries">
    <option value='0'>----------</option>
</select>
select id='store'>
    <option value='0'>----------</option>
</select>
</form>

目前,我可以用数据填充第二个选择框,但是,我不知道如何将其转换为“链接”,这样当被选中时,它将打开一个新窗口,其中包含我希望访问者转到的页面的链接。

有人能提供一些帮助或建议吗?

编辑:意思是,将XML中的“链接”改为<option>标签的“值”,例如。<option value="linkfromxml"> Retailer 2d </option>

并获取选择框以在新窗口中打开该链接。

EN

回答 2

Stack Overflow用户

发布于 2014-02-26 09:54:21

选择框不能显示带有链接的选项,这将是无效的HTML。浏览器将不知道如何处理标记。如果您正在寻找这种行为,则必须编写Javascript来根据用户与选择框的交互来更改document.location

例如:

代码语言:javascript
复制
$('select').change(function (ev) {
    var val = $(this).val();
    document.location = '/?value=' + val;
});
票数 0
EN

Stack Overflow用户

发布于 2014-02-26 09:56:48

为什么不在第二个select中添加一个更改侦听器呢?就像这样。

代码语言:javascript
复制
$('#store').change(function() { 
   document.location = $(this).val();
});

更新:

Demo here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22030014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档