gpt4 book ai didi

javascript - JQuery 在链式选择后选择多个

转载 作者:搜寻专家 更新时间:2023-11-01 04:40:57 25 4
gpt4 key购买 nike

用户应该在选择一些组之后选择主机。我用 JQuery chained remote Plugin 构建了一个链式选择通过组选择主机。正在使用以下代码并且工作正常:

$('#hosts').remoteChained({
parents: "#hosts_group",
url: "ajax/getHosts"
});
   <select id="hosts_group" name="hosts_group" class="form-control">
<option value="">Bitte Gruppe selektieren</option>
<option value="1>Some Groups</option>
</select>

<select id="hosts" name="hosts"></select>

但最终结果应该为主机提供一个双列表框,用户可以在其中从任何组中选择主机。我尝试将多个标签添加到主机选择并添加 JQuery DuallistBox通过以下代码段:

 $('#hosts').remoteChained({
parents: "#hosts_group",
url: "ajax/getHosts"
}).DualListBox({json: false});

双列表框显示正常,但当我选择组时没有显示主机。

JSON 数据如下所示:

[
{'name': 'host1', 'id': '1'},
{'name': 'host2', 'id': '2'}
]

选择不同的组时,json 还包含不同的主机。链式选择插件通过以下请求请求数据:ajax/getHosts/?hosts_group=selectedId

只需将链式选择与普通的多重选择结合使用就可以了。问题是在双列表框中显示 json 数据,每次选择都不同。

我尝试构建一个 JsFiddle 示例,但它不起作用,因为不会加载外部库,而且我真的不明白如何通过不同的选择手动提供 json。

最佳答案

好的,我想我已经按照您想要的方式工作了。问题源于 DualListBox 试图在 remoteChained 完成填充选择框之前初始化自身。理想的解决方案是在 remoteChained 完成时,初始化 DualListBox。不幸的是,remoteChained 似乎没有回调函数,这使得这有点棘手。这是我的(有点老套)解决方案:

$(document).ready(function() {
// setup remote chained
$('#hosts').remoteChained({
parents: "#hosts_group",
url: "ajax/getHosts"
});

// set up dualList when change is triggered
$('#hosts_group').on("change", function() {

// if the option wasn't the first one
if($(this).find("option:selected").val() != "") {
setUpDualList();
} else {
// left as an exercise
//putHostsSelectBoxBackIfItWasDestroyedByDualList();
}
});
});

function setUpDualList() {
if(!hostsChanged()) {
// because there's no callback, we have continually check if json is complete
setTimeout(function() { setUpDualList(); }, 300);
} else {
// this actually does it.
$("#hosts").DualListBox({json:false});
}
}

function hostsChanged() {
/*
* this is VERY simplistic. It will have to handle if #hosts was
* 'changed' to have the same content, or if it was changed to
* something that has the same number of options, etc. This ONLY
* works for the very simple case you presented above.
*/
return $("#hosts").find("option").length != 0;
}

HTML 可以保持不变:

<select id="hosts_group" name="hosts_group" class="form-control">
<option value="">Bitte Gruppe selektieren</option>
<option value="1">Some Groups</option>
</select>

<select id="hosts" name="hosts"></select>

但是,由于 DualListBox 具有破坏性,因此最好创建一个“hosts”选择和一个“hosts_destroyable”选择。可破坏的选择将简单地复制 ajax complete 上的“hosts”,初始化 DualList,并隐藏“hosts”。当需要再次隐藏 DualList 时(因为用户更改了选择的组),则需要重新创建“hosts_destroyable”。

所以。上面是简短的、hacky 的、是的,这是可行的,但它需要帮助的解决方案。下面是一个逐步的完整解决方案。

  • 初始化远程链接

  • 将#hosts_group 更改为有效组:

    1. $("#hosts").show();

    2. 监控#hosts 并确定 remoteChained 何时成功完成 json 请求。

    3. 请求完成后,将#hosts 复制到临时#hosts_temp

    4. $("#hosts_temp").DualListBox({json:false})

    5. $("#hosts").hide();

  • 关于将#hosts_group 更改为无效组(例如“Bitte Gruppe selektieren”):

    1. 销毁 DualListBox(不确定创建的是什么,如果存在,则以某种方式从 DOM 中删除)

    2. $("#hosts").show();

关于javascript - JQuery 在链式选择后选择多个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31003603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com