简体   繁体   English

在jqueryui DatePicker上设置'option'会清除文本框的值吗?

[英]Setting 'option' on jqueryui DatePicker clears the textbox's value?

I've highlighted the example here: 我在这里突出显示了这个例子:

http://jsfiddle.net/aDxeE/ http://jsfiddle.net/aDxeE/

Basically, a separate event (not known at construction) needs to set "option","maxDate" on the datepicker control. 基本上,一个单独的事件(在构造时不知道)需要在"option","maxDate"控件上设置"option","maxDate" This 'clears' the textbox value which is really annoying, as the initial construction does not. 这“清除”文本框值,这真的很烦人,因为初始构造没有。

Can anyone offer a solution? 有人能提供解决方案吗?

Edit: 编辑:

Changing line 5 to: 将第5行更改为:

$("#myinput").datepicker("destroy");
$("#myinput").datepicker({minDate:new Date(), maxDate: new Date()});

makes it work as expected, but is a seriously messy approach. 使它按预期工作,但是是一种严重混乱的方法。

It's an issue with the format. 这是格式的问题。 Your initial value dd-mm-yyyy doesn't match the datepickers default format. 您的初始值dd-mm-yyyy与datepickers默认格式不匹配。 To resolve, set the initial value to the correct format and specify the format as dateFormat when creating the datepicker. 要解决此问题,请将初始值设置为正确的格式,并在创建dateFormat时将格式指定为dateFormat

Fiddle 小提琴

Change to: 改成:

<input id="myinput" type="text" value="01-01-1970" />

And: 和:

//construct datepicker
$("#myinput").datepicker({minDate:new Date(), dateFormat : "dd-mm-yy"});
//later on, a seperate event changes the maxDate property
//but this 'clears' the existing value in the textbox!
$("#myinput").datepicker("option","maxDate", new Date());

You could do this: 你可以这样做:

$("#myinput").datepicker('destroy').datepicker({maxDate:new Date()});
  • Removes the datepicker functionality completely. 完全删除datepicker功能。 This will return the element back to its pre-init state. 这将使元素返回到其初始化前状态。
  • Again re-initiate the datepicker with setting the maxdate option. 再次使用设置maxdate选项重新启动maxdate

FIDDLE DEMO FIDDLE DEMO

UPDATE UPDATE

Formated the code for the readability purpose:- 为可读性目的编写代码: -

$("#myinput").datepicker("destroy");

$("#myinput").datepicker({
    minDate: new Date(),
    maxDate: new Date()
});

Try to give it in $(document).ready(function(){..}); 尝试在$(document).ready(function(){..});

//construct datepicker
$(document).ready(function(){
$("#myinput").datepicker({minDate:new Date()});
//later on, a seperate event changes the maxDate property
//but this 'clears' the existing value in the textbox!
});
$("#myinput").datepicker("option","maxDate", new Date());

Check JSFiddle . 检查JSFiddle

FYI: use placeholder instead of value placeholder="dd-mm-yyyy" 仅供参考:使用占位符代替值placeholder="dd-mm-yyyy"

How about manually setting it back again? 怎么样再次手动设置它?

var d = $("#myinput").val();
$("#myinput").datepicker("option", "maxDate", "+2d").val(d);

This question was already answered here : https://stackoverflow.com/a/8945264/2050459 这个问题已在这里得到解答: https//stackoverflow.com/a/8945264/2050459

You need to set the date first trough code(using setDate) or by opening the datepicker(user interaction). 您需要先通过代码设置日期(使用setDate)或打开datepicker(用户交互)。

Here is how you would do it with code : fiddle 以下是如何用代码完成它: 小提琴

$("#myinput").datepicker({
    minDate: new Date()
});
$("#myinput").datepicker("setDate", new Date());

In your example, try opening the datepicker and then trigger an event to set a maxDate, you'll see that the input will not be cleared because an active date has been set. 在您的示例中,尝试打开datepicker,然后触发事件以设置maxDate,您将看到输入将不会被清除,因为已设置了活动日期。

Jqueryui DatePicker clears the textbox's value? Jqueryui DatePicker清除文本框的值?

myinput = ID of datapicker

$("#myinput").val('');

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

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