javascript 设置日期选择器的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31202132/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:26:47  来源:igfitidea点击:

Set value of datepicker?

javascriptjquerydatepickerjquery-ui-datepicker

提问by bob

I've explored maybe 30 threads and my problem still isn't solved.

我已经探索了大约 30 个线程,但我的问题仍未解决。

I have a JQuery datepicker:

我有一个 JQuery 日期选择器:

<input id="datepicker" name="date" class="mydatepicker" onchange="isValidDate()"></input>

I simply want to write a javascript function that accomplishes this when called:

我只想编写一个 javascript 函数,在调用时完成此操作:

<script type="text/javascript">
document.getElementById("datepicker").value = 'certain date';
</script>

datepicker is tied to these files:

datepicker 与这些文件相关联:

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

回答by Rory McCrossan

You need to use the setDatefunction of the datepickerlibrary. Try this:

您需要使用库的setDate功能datepicker。试试这个:

$('#datepicker').datepicker('setDate', new Date()); // = set to today