Jquery datepicker - 手动触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8107013/
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
Jquery datepicker - manual triggering
提问by Ondrej
I'm using a jquery datepicker in one of my project and I need to trigger the datepicker by clicking on a hyperlink which is next to my input area.
我在我的一个项目中使用了 jquery 日期选择器,我需要通过单击输入区域旁边的超链接来触发日期选择器。
Unfortunately I can't get this working for some reason.
不幸的是,由于某种原因,我无法使其正常工作。
I have an input area and the hyperlink I would like to trigger a calendar in the input area.
我有一个输入区域和超链接,我想在输入区域触发日历。
<input type="text" id="date" /><a href="#" class="trigger">Show calendar</a>
I've tried to come up with a Javascript code such as:
我试图想出一个 Javascript 代码,例如:
$(".trigger").click(function(){
$("#date").datepicker("show");
});
As someone else in one of previous discussion suggested to use a SHOW method to make the calendar pop up. But it doesn't work.
正如之前讨论中的其他人建议使用 SHOW 方法使日历弹出。但它不起作用。
Code such as
代码如
$(function() {
$("#date").datepicker();
});
works fine, but just by clicking into an input area (not on triggering hyperlink).
工作正常,但只需单击输入区域(而不是触发超链接)。
Does anyone have any idea or suggestion how to get this working? Becouse as far as I know I'm not the only one chasing the solution.
有没有人有任何想法或建议如何让这个工作?因为据我所知,我并不是唯一一个寻求解决方案的人。
Thank you very much! ;)
非常感谢!;)
回答by Robert Van Sant
Is your click()
event function in the jQuery(document).ready()
function?
你的click()
事件函数在jQuery(document).ready()
函数中吗?
Because jQuerywill recursively assign the click event handler to each class that you defined when the page loads.
因为jQuery会递归地将单击事件处理程序分配给您在页面加载时定义的每个类。
Otherwise, it won't do anything if it's sitting outside the document ready function, like so:
否则,如果它位于文档就绪函数之外,它将不会执行任何操作,如下所示:
$(function() {
$("#date").datepicker();
$(".trigger").click(function(){
$("#date").datepicker("show");
});
});
also I'll assume you have jQueryloaded and the datepicker plugin loaded in the head of the HTMLdocument :)
另外我假设你已经加载了jQuery并且在HTML文档的头部加载了 datepicker 插件:)
回答by Romario
I use this:
我用这个:
$(".ui-datepicker-current-day").trigger("click");
$(".ui-datepicker-current-day").trigger("click");