【发布时间】:2015-11-05 21:24:43
【问题描述】:
希望你们能帮我解决我遇到的一个小问题
所需功能: 我正在尝试制作一个跨浏览器可折叠的多选框,它仅在框折叠时显示所选选项(鼠标退出选择时折叠),然后在框展开(鼠标悬停)并保留状态时将它们全部恢复的检查项目。有关所需功能,请参阅底部的 Fiddle(仅限 Firefox)
问题: 问题是检查状态似乎没有记录在 HTML 中,它可能是 GET/POST 数据的形式,如果是,如何访问它。无论是那个还是我错过了什么或做错了什么,这很可能是它不起作用的原因;-)
需要帮助:有没有办法恢复多选的选项及其先前选择(选中)的状态?
jsFiddle jQuery collapsible select box
function removeOptions($select) {
var $optionsToRemove = $select.find('option:not(:selected)'); //filter for non selected options
$optionsToRemove.remove(); //remove
}
function setSelectCurrentState($select) {
$select.data("currentHTML", $select.html()); //save the current state (this does not work for multiple)
}
function restoreOptions($select) {
var ocHTML = $select.data("currentHTML"); //retrieve the data
if (ocHTML != undefined) {
$select.html(ocHTML); //restore (the state is not sotred in the html so this doesn't work)
}
}
$(document).ready(function () {
var $hoverSelect = $('#hoverSelect');
/*As we leave the select box store the current state and then remove filtered options*/
$hoverSelect.mouseleave(function () {
setSelectCurrentState($hoverSelect); // save the current state
removeOptions($hoverSelect); //remove options
});
/*When we hover back over the select restore all options with their selected states*/
$hoverSelect.mouseenter(function () {
restoreOptions($hoverSelect);
});
});
如果你在这个小提琴中认出了你的代码,很抱歉没有记下你,但我丢失了链接。
我也有类似的东西,它只使用 CSS 并且在 Firefox 中运行良好,但由于 IE 和 Edge 不允许设置选项 display:none; 它在这些浏览器。此代码将举例说明如果您使用 Firefox 查看它,我希望它如何工作。
【问题讨论】:
标签: jquery select cross-browser show-hide