首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用另一个带有javascript的SELECT多个值更新SELECT多个列表(使用erichynds multiselect小部件)

使用另一个带有javascript的SELECT多个值更新SELECT多个列表(使用erichynds multiselect小部件)
EN

Stack Overflow用户
提问于 2013-05-20 17:54:14
回答 1查看 437关注 0票数 0

我有两个多选择框(使用插件)来显示几个不同部分中的参数列表。

代码语言:javascript
复制
<select id="select_A" multiple="multiple">
    <option value="sec-1" id="main1">section1</option>
    <option value="sec-2" id="main2">section2</option>
    <option value="sec-3" id="main3">section3</option>
</select>

<select id="select_B" multiple="multiple">
    <optgroup label="section-1" id="sub1">
        <option value="1A">1A</option>
        <option value="1B">1B</option>
    <optgroup label="section-2" id="sub2">
        <option value="2A">2A</option>
        <option value="2B">2B</option>
    <optgroup label="section-3" id="sub3">
        <option value="3A">3A</option>
        <option value="3B">3B</option>
</select>

在区段列表中,我选择几个,然后通过表单get和submit输入传递值来填充参数列表。

我想摆脱“单击提交”的步骤,动态填充第二个列表,即:如果选择section1和section3,第二个列表将填充section1和section3及其各自的子选项,如果添加section2,则刷新第二个列表并填充第1、2、3节及其子选项。

有什么方法可以用javascript来完成吗?

与我想做的事情无关,但是为什么在#select_B上应用插件(取消注释这些行)时,这些函数不能工作呢?有冲突吗?http://jsfiddle.net/8T6fU/10/

代码语言:javascript
复制
$(function(){
    $("#select_B").multiselect({
    noneSelectedText: "Choose Parameters"
    }).multiselectfilter();
});

-----------------Edit-----------------

这就是我试过的:

put /8T6fU/22/(删除了http,因为我不能将两个链接放在同一个帖子中)

代码语言:javascript
复制
function TestSubCat()
{
    var select = document.getElementById("SubCat");
    var optG = document.createElement("OPTGROUP");
    var G = select.getElementsByTagName("optgroup");

    //var optGExists = G[0].length < 0;
    //if(typeof G[0].label != null){alert("undef");}
    //alert(G[0].label);
    var optGExists = select.length < 0;

    if(document.getElementById("Category").options[0].selected == true && !optGExists)
    {          
        if(optG.label !== "FirstCat")
        {
            optG.label = "FirstCat";
            select.appendChild(optG);
            optG.appendChild(new Option("reboot","FirstCat[reboot]"));
            optGExists = true;
        }
    }
    if(document.getElementById("Category").options[0].selected == false)
    {
        select.remove(0);
        optGExists = false;
    }

    if(document.getElementById("Category").options[1].selected == true)
    {
        if(optG.label !== "SecondCat")
        {
            optG.label = "SecondCat";
            select.appendChild(optG);
            optG.appendChild(new Option("Port","SecondCat[Port]"));
        }
    }
    if(document.getElementById("Category").options[1].selected == false)
    {
        select.remove(1);
    }

    $("#SubCat").multiselect("refresh");
}

我知道optGExists是错的,但不能纠正..。基本上,如果optgroup标签已经存在于SubCat列表中,我希望避免追加。

我对select.remove()也有一个问题。为了测试目的,我只是硬编码索引。如何删除与右optgroup相关的值?

EN

回答 1

Stack Overflow用户

发布于 2013-05-20 18:16:23

我会为第一个复选框执行一个:

代码语言:javascript
复制
$('#select_A').on('change', function(){
    $("#select_B").multiselect({
         noneSelectedText: "Choose Parameters"
    }).multiselectfilter();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16655337

复制
相关文章

相似问题

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