添加jquery datepicker选项后,HTML输入值是否消失?

时间:2016-07-28 12:42:37

标签: javascript jquery html datepicker

我有多个文字输入,如下所示:

<input type="text" class="datepicker" value="11-11-2016">

和jquery脚本:

  $(".datepicker").each(function() {
    $(this).datepicker();
    $(this).datepicker("option", "changeYear", true);
  });

页面加载后输入值立即消失!!!

如果我删除选项:

$(this).datepicker("option", "changeYear", true);

...值不会消失,但每当我添加任何jquery datepicker选项时 - 值消失!?

5 个答案:

答案 0 :(得分:1)

之后尝试使用此行来解决问题:

SELECT * FROM
   (SELECT DISTINCT
        [dbo].[ttcibd001110].[t_cmnf] AS [Manufacturer],
        [dbo].[ttcibd001110].[t_item] AS [Item code],
        [dbo].[ttcibd001110].[t_dsca] AS [Description],
        [dbo].[ttcibd001110].[t_seak] AS [Search key 1],
        [dbo].[twhinr110110].[t_trdt] AS [Transaction date],
        [dbo].[twhinr110110].[t_cwar] AS [Warehouse],
        [dbo].[twhinr110110].[t_qstk] AS [Quantity Inventory Unit]

        FROM [dbo].[twhinr110110] LEFT JOIN [dbo].[ttcibd001110]
            ON [dbo].[twhinr110110].[t_item]=[dbo].[ttcibd001110].[t_item]
            WHERE [dbo].[twhinr110110].[t_koor]='2' AND [dbo].[ttcibd001110].[t_cmnf]='ManufacturerX') AS tabel 
WHERE ltrim(tabel.[Item code])='1000045'

如果属性是$(".datepicker").each(function() { $(this).datepicker(); $(this).datepicker("option", "changeYear", true); $(this).attr("value", "11-11-2016"); }); 的动态属性,请使用以下内容:

$(".datepicker")

将所有属性作为对象获取。所以:

function getElemAttributes(var element) {
    var attrs = {};
    var attrMap = element.attributes;

    $.each(attrMap, function (i, e) { attrs[e.nodeName] = e.nodeValue; });

    return attrs;
}

答案 1 :(得分:0)

我认为你初始化时遇到的问题,试试这个:

$(function(){

    $('.datepicker').each(function(){
        $(this).datepicker({"changeYear": true});
    });

});

这是一个有效的fiddle

答案 2 :(得分:0)

您应该使用此选项设置初始日期:

$( ".selector" ).datepicker( "setDate", "10/12/2012" );

代码示例:

  $(".datepicker").each(function() {
    $(this).datepicker();
    $(this).datepicker("option", "changeYear", true);
    $(this).datepicker( "setDate", "10/12/2012" );
  });

答案 3 :(得分:0)

您需要这个:

$(document).ready(function () {

$('.datepicker').each(function(){
    $(this).datepicker();
    $(this).datepicker("option", "changeYear", true);
    $(this).datepicker( "setDate", $(this)[0].getAttribute('value') );
});

答案 4 :(得分:0)

这个命令帮助我解决了像你这样的问题:

let date = $(this.dateInput.nativeElement).val().toString();
....
$(this.dateInput.nativeElement).datepicker().val(date).trigger('change');