3

我正在尝试获取 datepicker 的按钮文本(默认为省略号),以将其自身设置为输入的值。

我在页面上有多个日期选择器,与下面的示例不同,我有一个图标而不是文本输入。我希望值本身显示在您将鼠标悬停在图标上时出现的工具提示上。

我已经尝试过 -buttonText: $(this).val()作为一种选择,但它没有任何反应

示例 - http://jsbin.com/unolot/3/

所以我的问题- 如何让日期选择器输入的按钮文本动态匹配值?

4

1 回答 1

3

您必须处理两种情况:

  • When the selected date changes: use the onSelectevent to change the button's title attribute
  • 在初始渲染时:不幸的是,日期选择器还没有实现小部件工厂,因此没有create事件,您必须在小部件初始化后手动更新标题属性。

不要忘记将picker的默认日期设置为字段的初始值,否则默认选择的日期是“今天”

像这样的东西:

// as you have several instances, loops through them
$(".deadline_picker_on").each(function() {

    var dt = $(this);

    dt.datepicker({
        showOn: "button",
        buttonText: '',
        // set the default date to the value of the field
        defaultDate: dt.val(),
        duration: 100,
        dateFormat: 'yy-mm-dd',
        showOptions: {
            direction: 'right'
        },
        onSelect: function(dateText, inst) {
            // on data selection, change the title attribute of the button
            $(this).next().attr('title', dateText);
        }
    })
    // get the button (next element)
    .next()
    // set the title attribute with the formatted initial date from the picker
    .attr('title', $.datepicker.formatDate('yy-mm-dd', dt.datepicker('getDate')));
});
​
于 2012-06-07T11:06:18.917 回答