javascript 从另一个函数调用 jQuery UI Datepicker

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

Call jQuery UI Datepicker from another function

javascriptjqueryjquery-uistruts

提问by Panadol Chong

The following is my code:

以下是我的代码:

<script type="text/javascript">
$(function(){ //this is the datepicker function
    $( "#datepicker" ).datepicker();
});

function jsFunction(){
   // i want to call the datepicker function from here
}
</script>
</head>

html

html

<html:image alt="Calendar" src="/images/icon_calendar.gif" 
    value="reset" onclick="jsFunction();" />
<input type="text" id="datepicker" />

When I click on the text box, the text box will prompt a calendar to let me choose the date. This date picker is working fine.

当我点击文本框时,文本框会提示一个日历让我选择日期。这个日期选择器工作正常。

I added in an image, and I want the text box to prompt the calendar when I click on the image instead of click on the text box. I would like to ask how to call the datepicker jquery from the onlick=jsFunction(); .

我添加了一个图像,我希望文本框在单击图像而不是单击文本框时提示日历。我想问一下如何从onlick=jsFunction();调用datepicker jquery .

I an working in Java Struts2 framework.

我在 Java Struts2 框架中工作。

Kindly advise/ help.

请建议/帮助。

回答by Samuel Liew

Correct way to set an icon trigger (via API):

设置图标触发器的正确​​方法(通过 API):

$( "#datepicker" ).datepicker({
    showOn: "button",
    buttonImage: "/images/icon_calendar.gif",
    buttonImageOnly: true
});

http://jqueryui.com/datepicker/#icon-trigger

http://jqueryui.com/datepicker/#icon-trigger

回答by Arun P Johny

You can use the show method

您可以使用show 方法

function jsFunction(){
    $( "#datepicker" ).datepicker( "show" );
}

Demo: Fiddle

演示:小提琴

回答by Musa

Try

尝试

function jsFunction(){
    $( "#datepicker" ).focus();
}

or

或者

function jsFunction(){
    $( "#datepicker" ).click();
}

回答by user3024508

<script>

    $(function() {

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

    });

</script>

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