簡體   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