javascript 将日期设置为 Jquery UI 日期选择器输入文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20542806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Date to the Jquery UI datepicker input textbox
提问by Arun
I am using jquery ui datepicker.and its working fine .my need is to set the date to the textbox when querystring contain any date
我正在使用 jquery ui datepicker.and 它的工作正常。我需要在查询字符串包含任何日期时将日期设置为文本框
eg my url is localhost:301/?date=2013-12-01
例如我的网址是 localhost:301/?date=2013-12-01
so I want to set this date to input textbox.
所以我想设置这个日期来输入文本框。
my input box html is
我的输入框 html 是
<input type="text" id="datepicker" class="txt-cal hasDatepicker" name="addFromDate" date_name="2013-12-11">
I tried some thing but no result.
我尝试了一些东西,但没有结果。
$(function () {
var myDate = ?/*how to select the date_name from input textbox
$('#datepicker').datepicker();
$('#datepicker').datepicker('setDate', myDate);
$('#datepicker2').datepicker();
$('#datepicker2').datepicker('setDate', myDate);
$('#datepicke3').datepicker();
$('#datepicke3').datepicker('setDate', myDate);
$('#datepicker4').datepicker();
$('#datepicker4').datepicker('setDate', myDate);
});
Thanks in advance
提前致谢
回答by Praveen
A better approach will be using custom data
attribute.
更好的方法是使用自定义data
属性。
data-date="2013-12-11"
Now you can access it
现在你可以访问它
var myDate = $("#datepicker").data("date");
$('#datepicker').datepicker('setDate', myDate);
Update:
更新:
I doubt you're trying to get it from url(ie, query string)
我怀疑您是在尝试从 url(即查询字符串)中获取它
If so check this answer, where a method is written to get
如果是这样,请检查此答案,其中编写了一个方法来获取
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Now
现在
var myDate = getParameterByName("date");
回答by CodingIntrigue
You need to use the .attr function:
您需要使用 .attr 函数:
var myDate = $("#datepicker").attr("date_name");
However, you should beware of creating non-standard HTML attributes
但是,您应该注意创建非标准的 HTML 属性
回答by Anup Pandey
You can do this:
你可以这样做:
$('#datepicker1').datepicker({ orientation: 'top', format: 'dd-mm-yyyy' });
$('#datepicker1').datepicker({orientation: 'top', format: 'dd-mm-yyyy' });
var currentDate = new Date();
var day = ((currentDate.getDate()).toString().length == 1) ? '0' + (currentDate.getDate()).toString() : (currentDate.getDate()).toString();
var month = ((currentDate.getMonth() + 1).toString().length == 1) ? '0' + (currentDate.getMonth() + 1).toString() : (currentDate.getMonth() + 1).toString();
var year = currentDate.getFullYear();
$("#datepicker1").val(day + '-' + month + '-' + year);
I have tried in text input control. It just needed to apply datepicker 1st then set the current date to textbox.
我在文本输入控件中尝试过。它只需要应用 datepicker 1st 然后将当前日期设置为文本框。