繁体   English   中英

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

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

我在这里突出显示了这个例子:

http://jsfiddle.net/aDxeE/

基本上,一个单独的事件(在构造时不知道)需要在"option","maxDate"控件上设置"option","maxDate" 这“清除”文本框值,这真的很烦人,因为初始构造没有。

有人能提供解决方案吗?

编辑:

将第5行更改为:

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

使它按预期工作,但是是一种严重混乱的方法。

这是格式的问题。 您的初始值dd-mm-yyyy与datepickers默认格式不匹配。 要解决此问题,请将初始值设置为正确的格式,并在创建dateFormat时将格式指定为dateFormat

小提琴

改成:

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

和:

//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());

你可以这样做:

$("#myinput").datepicker('destroy').datepicker({maxDate:new Date()});
  • 完全删除datepicker功能。 这将使元素返回到其初始化前状态。
  • 再次使用设置maxdate选项重新启动maxdate

FIDDLE DEMO

UPDATE

为可读性目的编写代码: -

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

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

尝试在$(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());

检查JSFiddle

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

怎么样再次手动设置它?

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

这个问题已在这里得到解答: https//stackoverflow.com/a/8945264/2050459

您需要先通过代码设置日期(使用setDate)或打开datepicker(用户交互)。

以下是如何用代码完成它: 小提琴

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

在您的示例中,尝试打开datepicker,然后触发事件以设置maxDate,您将看到输入将不会被清除,因为已设置了活动日期。

Jqueryui DatePicker清除文本框的值?

myinput = ID of datapicker

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

暂无
暂无

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

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