gpt4 book ai didi

javascript - 用于关闭和打开选择下拉列表的 Jquery 事件,而不是在更改时

转载 作者:行者123 更新时间:2023-11-28 00:03:01 24 4
gpt4 key购买 nike

我想要一个简洁的解决方案来处理下拉菜单的事件,以便当用户打开选择菜单时,它会提醒打开,当他关闭它时,它会提醒关闭,忽略所选值是否更改。

<select id="dummy">
<option>dummy1</option>
<option>dummy2</option>
<option>dummy3</option>
</select>

我想要的是类似的东西

$("#dummy").on('open',function(){//do something})
$("#dummy").on('close',function(){//do something})

类似heapbox的东西 http://www.bartos.me/heapbox/

并且此解决方案是 Not Acceptable :Run change event for select even when same option is reselected

最佳答案

扩展选择框的 native 功能的典型方法是用可设置样式的标记替换它,然后将新标记的值绑定(bind)回原始(现已隐藏)选择元素。 (注意:我没有包含任何样式。这是使用选择替换的简单示例)。

var SelectBox = {
init: function () {
if ($('select').length > 0) {
this.generateStyledSelectbox('custom-select');
};
},

generateStyledSelectbox: function (cssClass) {

// Contained within .each to isolate all instances of <select>
$('select').each(function(index) {
var $source = $(this),
selected = $source.find("option[selected]"),
options = $source.find('option'),
selindex = index;

// Append styleable pseudo-select element to doc
$source.after('<div id="result-' + index + '" class="' + cssClass + '"></div>');

// Construct select list in pseudo select element
$('#result-' + index).append('<dl id="activeValue-' + index + '" class="dropdown"></dl>');
$('#activeValue-' + index).append('<dt><a href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a></dt>');
$('#activeValue-' + index).append('<dd><ul></ul></dd>');

// Assign select values to pseudo-select lis items
options.each(function () {
$('#activeValue-'+ index + ' dd ul').append('<li class="select-menu-item"><a href="#">' + $(this).text() + '<span class="value">' + $(this).val() + '</span></a></li>');
});

$('.dropdown').each(function(index) {
$(this).find('dd ul li a').on('click', function(event) {
event.preventDefault();
var text = $(this).not('.value').html(),
$base = $('.custom-selectbox').eq(index);

$('.dropdown').eq(index).find('dt a').html(text);
$('.dropdown').eq(index).find('dd ul').hide();

$base.val($(this).find('span.value').html());
});
});

// prevent link actions in dropdown
$('.dropdown dt a').on('click', function (event) {
event.preventDefault();
});

// open/close
$(".dropdown").eq(index).find('dt a').on('click', function () {
$(".dropdown").eq(index).find('dd ul').toggle();
});

$(".dropdown").eq(index).find('dd ul li a').on('click', function () {
var text = $(this).html(),
newval = $(this).find('.value').html();

$(".dropdown").eq(index).find('dt a span').html(text);
$('select').eq(index).val(newval);
$(".dropdown").eq(index).find('dd ul').hide();
});

// Hide dropdown on outside click
$(document).on('click', function (e) {
var $clicked = $(e.target);
if (!$clicked.parents().hasClass("dropdown")) {
$(".dropdown").eq(index).find('dd ul').hide();
}
// remove dropdown-open targetable class
if (!$clicked.parents().hasClass("dropdown-open")) {
$clicked.parents().removeClass('dropdown-open');
}
});

// Hide native select
$source.css('display', 'none');

// assign initial (default) value
var initialval = $source.find('option').eq(0).html();
$('#activeValue-'+index+' dt a').html(initialval);

}); // END .each
}
};
SelectBox.init();

这是一把 fiddle http://jsfiddle.net/P6ZCn/ (同样,没有样式)

关于javascript - 用于关闭和打开选择下拉列表的 Jquery 事件,而不是在更改时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20321553/

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