【问题标题】:jQuery datepicker: Convert select dropdown into uljQuery datepicker:将选择下拉列表转换为 ul
【发布时间】:2017-01-15 22:13:42
【问题描述】:

我正在使用 jquery-ui datepicker 组件,但是 monthyear 下拉列表是 select 标签。

我需要以选择元素无法实现的方式设置它们的样式,因此我想将它们转换为 ul 元素。

任何帮助将不胜感激 - 这是一个带有 jquery-ui datepicker https://jsfiddle.net/GeekOnGadgets/wra3pcsv/ 的入门 jsFiddle

【问题讨论】:

  • 为什么你没有使用ui-datepicker-year & ui-datepicker-month 类来应用样式?
  • 不起作用,需要将选择悬停在不同的颜色和其他要求,这是用选择改变的东西。
  • 我添加:.ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default{ 背景:#ffff00; } .ui-state-default:hover, .ui-widget-content:hover .ui-state-default:hover, .ui-widget-header:hover .ui-state-default:hover{ 背景: #ff0000; } 到你的 CSS 和它的工作。我认为您需要在此日历声明时调用适当的类。谢谢
  • @AmranurRahman 我认为您误解了这个问题。我想更改下拉菜单。
  • 清理了语法并解释了无法对 cme​​ts 中提到的选择进行所需的样式。

标签: javascript jquery html css datepicker


【解决方案1】:

以下代码可能对您有所帮助。你可以在这个jsFiddle看到结果。

// Replace a select element by a ul element
var convertToList = function (inst, className) {
    var $selectElement = inst.dpDiv.find('.' + className);
    var $listElement = $('<ul class="{0} dateList"></ul>'.replace('{0}', className + '-list')).appendTo($selectElement.parent());
    $selectElement.children(' option').each(function () {
        // Replace each option of the select element by a li element
        $listElement.append('<li class="dateListItem" value="{0}">{1}</li>'.replace('{0}', $(this).val()).replace('{1}', $(this).text()));
    });
    $listElement.find('.dateListItem').click(function () {
        // When an item is clicked, set the corresponding value in
        // the original select element and trigger the change event
        $selectElement.val($(this).val());
        $selectElement.change();
    }).each(function () {
        // Add the selectedValue class to the selected item
        if ($(this).val() == $selectElement.val()) {
            $(this).addClass('selectedValue');
        }
    });
};

// Replace the month and year select elements
var convertToDatePickerWithLists = function (inst) {
    setTimeout(function () {
        inst.dpDiv.addClass('datePickerWithLists');
        convertToList(inst, 'ui-datepicker-month');
        convertToList(inst, 'ui-datepicker-year');
    }, 0);
};

// Associate the datepicker to the text input element
$("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true,
    beforeShow: function (input, inst) {
        // Modify the datepicker when it is initially displayed
        convertToDatePickerWithLists(inst);
    },
    onChangeMonthYear: function (year, month, inst) {
        // Modify the datepicker every time the month/year is changed
        convertToDatePickerWithLists(inst);
    }
});

以下是各种类的 CSS 样式:

.datePickerWithLists .ui-datepicker-year,
.datePickerWithLists .ui-datepicker-month
{
    display: none; /* Hide the original select elements */
}

.datePickerWithLists .ui-datepicker-header
{
    height: 20em;
    background: #D0D0D0;
}

.dateList
{
    display: inline-block;
    list-style: none;
    font-size: 12px;
    vertical-align: top;
    margin: 0px;
    padding: 8px 0px 0px 0px;
    text-align: center;
    width: 40%;
}

.dateListItem
{
    line-height: 1.4em;
    cursor: pointer;
}

.dateListItem.selectedValue
{
    background-color: black;
    color: white;
}


更新

在讨论了GeekOnGadgets的具体需求后,代码和样式已经进行了调整,如this jsfiddle所示。

【讨论】:

  • 感谢您的回答 :)。我试图改变你所做的,但卡在选择月份和年份,然后关闭下拉菜单。尝试了几件事,但没有。你介意看看我正在做的小提琴吗?希望得到一些帮助https://jsfiddle.net/GeekOnGadgets/wra3pcsv/12/
  • 我想弄清楚你想要得到什么结果。您只是想要两个下拉列表吗?还是一个包含月份和年份的下拉列表?
  • 好的,我正在努力。顺便说一句,Dekel 的解决方案非常接近您想要的,不是吗?
  • 您可以在 jsfiddle.net/ConnorsFan/wra3pcsv/14 找到 jsfiddle 的更新版本。我修复了日期选择器标题的高度以避免过度闪烁。当我在 VS2015 中使用 jquery 3.1.0 进行测试时,日历不会超过 2016 年 12 月(它在 jsfiddle 中执行)。您可以考虑为列表设置z-index,以避免其他一些故障。
  • 非常感谢!你是一位天才程序员。没有超过 2016 年的原因是因为yearRange: '-80:+0'。谢谢。
【解决方案2】:

这个解决方案主要基于@ConnorsFan 的解决方案(你的解决方案得到了我的投票),但是如果你想有一个基于 jQuery ui 的selectmenu 的解决方案(所以你可以根据jQuery ui 的主题)你可以使用这个代码:

const fullMonthNames = ['Janurary', 'Februbary', 'March', 
    'April', 'May', 'June', 'July', 'August',
    'September', 'October', 'Novermber', 'December'];

function updateToSelectMenu() {
    $('.ui-datepicker-title select').selectmenu({
        select: function(e) {
        $(this).trigger('change');
        updateToSelectMenu();
        }
    })
    $('.ui-datepicker-title').append($('.ui-selectmenu-menu'));
}

$('#datepicker').datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel: this.todayButton,
    yearRange: '-80:+0',
    monthNamesShort: fullMonthNames,
    beforeShow: function() {
        setTimeout(function() {
            updateToSelectMenu()
        }, 0);
    },
    onChangeMonthYear: function() {
        setTimeout(function() {
            updateToSelectMenu()
        }, 0);
    }
});

selectmenu 是在 jQuery ui v1.11 中添加的,因此请确保使用正确的版本。

这是一个工作示例:

const fullMonthNames = ['Janurary', 'Februbary', 'March', 
												'April', 'May', 'June', 'July', 'August',
                        'September', 'October', 'Novermber', 'December'];
                        
function updateToSelectMenu() {
	$('.ui-datepicker-title select').selectmenu({
    select: function(e) {
      $(this).trigger('change');
      updateToSelectMenu();
    }
  })
  $('.ui-datepicker-title').append($('.ui-selectmenu-menu'));
}
 $('#datepicker').datepicker({
   changeMonth: true,
   changeYear: true,
   showButtonPanel: this.todayButton,
   yearRange: '-80:+0',
   monthNamesShort: fullMonthNames,
   beforeShow: function() {
   	setTimeout(function() {
    	updateToSelectMenu()
    }, 0);
   },
   onChangeMonthYear: function() {
   	setTimeout(function() {
    	updateToSelectMenu()
    }, 0);
   }
 });
select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

.ui-datepicker-month, .ui-datepicker-year {
  border: none;
  background-color: white;
  border-color: none;
}

#ui-datepicker-div .ui-selectmenu-menu, #ui-datepicker-div .ui-selectmenu-button{
  width: 29%;
  font-size: 13px;
}
.ui-menu { 
  max-height: 420px !important; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
<p>Date: <input type="text" id="datepicker"></p>

【讨论】:

  • 非常好的解决方案!
猜你喜欢
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多