如何触发 jQuery UI DatePicker onclick?

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

How to fire jQuery UI DatePicker onclick?

jqueryjquery-ui

提问by ihtus

The standard code for DatePicker is:

DatePicker 的标准代码是:

<script>
    $(function() {
       $( "#datepicker" ).datepicker();
    });
</script>
<p>Date: <input type="text" id="datepicker"></p>

How to launch datepicker onclick event from inline html, I mean

如何从内联 html 启动 datepicker onclick 事件,我的意思是

<input ... onclick="" >

, and with no separate JS code?

,并且没有单独的 JS 代码?

I tried:

我试过:

<p>Date: <input type="text" id="datepicker" onclick="$(function() {$('#datepicker').datepicker();});"></p>

But it's working only on second click.

但它只适用于第二次点击。

How to make it work on first click?

如何让它在第一次点击时工作?

Thanks.

谢谢。

采纳答案by Nick

<p>Date: <input type="text" id="dp" onclick="$('#dp').datepicker();$('#dp').datepicker('show');"></p>?

That work by any chance?

那工作有机会吗?

I'm no expert, but I believe you were having to click it twice because the first click merely initialised it as a datepicker. After this initialising, it's behaving as a normal datepicker, regardless of the onclick.

我不是专家,但我相信您必须单击它两次,因为第一次单击只是将其初始化为日期选择器。在这个初始化之后,它的行为就像一个正常的日期选择器,不管 onclick 是什么。

回答by charlietfl

After you create datepicker, open it this way:

创建日期选择器后,以这种方式打开它:

 $('#datepicker').datepicker('show')

Go to datepicker API docs look at methods tab, it's all there

转到 datepicker API 文档查看方法选项卡,就在那里

回答by user3024508

Try this code:

试试这个代码:

<script>
$(function() {
    $("#datepic").datepicker({ showOn: 'button',buttonImage: 'image/calendar.gif'});

});
</script>

<input type="text" id="datepic" /> 

回答by Bert