简体   繁体   English

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

[英]HTML input value disappears after adding jquery datepicker option?

I have multiple text inputs like this:我有多个这样的文本输入:

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

And the jquery script:和 jquery 脚本:

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

The input value disappears immediately after the page loads!!!页面加载后输入值立即消失!!!

If I remove the option:如果我删除该选项:

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

... the value doesn't dissapear, but whenever I add any of the jquery datepicker options - the value dissapears!? ...该值不会消失,但是每当我添加任何 jquery datepicker 选项时,该值就会消失!?

Try this line afterwards to rectify the issue:之后尝试此行以纠正问题:

$(".datepicker").each(function() {
    $(this).datepicker();
    $(this).datepicker("option", "changeYear", true);
    $(this).attr("value", "11-11-2016");
});

If the attributes are dynamic per $(".datepicker") , then use the following:如果属性是动态的$(".datepicker") ,则使用以下内容:

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

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

    return attrs;
}

to get all of the attributes as an object.将所有属性作为对象获取。 So:所以:

$(".datepicker").each(function() {
    $(this).datepicker();

    // Returns something like { id: "datepicker", ..., value: "11-11-2016" }
    var originalAttributes = getElemAttributes(this);

    // Do stuff that affects attr on element.
    $(this).datepicker("option", "changeYear", true);

    // Set element attributes from riginal attributes object.
    if (originalAttributes.hasOwnOroperty("value")) {
        $(this).attr("value", originalAttributes["value"]);
    }
    else {
        // Didn't originally have a "value" attribute - set some default here?
    }
});

I think the problem it's in your initialization, try this:我认为问题出在你的初始化上,试试这个:

$(function(){

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

});

Here's a working fiddle这是一个工作小提琴

You should use this option to set the initial date:您应该使用此选项来设置初始日期:

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

in you code example:在您的代码示例中:

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

You need this:你需要这个:

$(document).ready(function () {

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

This command helped me with the problem like yours:这个命令帮助我解决了像你这样的问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM