如何在按钮单击时显示 jquery datepicker

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

how to show a jquery datepicker on button click

jqueryjquery-ui

提问by JN_newbie

I want to show a datepicker only on button click

我只想在单击按钮时显示日期选择器

<input type="button" id=day value="day" />
$("#day").datepicker();

the above line fills the button text with the selected date

上面的行用选定的日期填充按钮文本

I have also tried with below code but nothing happens

我也试过下面的代码,但没有任何反应

$("#day").click(function() {
    $("#hiddenField").datepicker('show');
});

回答by Praxis Ashelin

You can do this with the built-in showOnfunction:

您可以使用内置showOn函数执行此操作:

$( "#hiddenField" ).datepicker({
    showOn: "button",
    buttonText: "day"
});

JSFiddle: http://jsfiddle.net/Uv8V4/

JSFiddle:http: //jsfiddle.net/Uv8V4/

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

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

回答by Anjana Silva

You can attach a button as a trigger to the jQuery DatePicker as mentioned below,

您可以将按钮作为触发器附加到 jQuery DatePicker,如下所述,

<script type="text/javascript>"
  $( function() {
      $( "#datepicker" ).datepicker({
          showOn: "button",
          buttonImage: "https://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
          buttonImageOnly: true,
          buttonText: "Select date"
      });
    });
    </script>

Click on the following link for a working fiddle example

单击以下链接以获取工作小提琴示例

https://jsfiddle.net/anjanasilva/rtt25a1m/

https://jsfiddle.net/anjanasilva/rtt25a1m/

回答by sidhantgupta

try following :

尝试以下:

$("#day").click(function() {
        $("#hiddenField").datepicker("show");
    });

single quotes doesn't work.

单引号不起作用。

hope this helps.

希望这可以帮助。

回答by MysticMagic?

Try putting

尝试放置

$("#hiddenField").datepicker({
    showOn: 'button',
    buttonText: 'Choose Date',
    dateFormat: 'dd/mm/yy',
    constrainInput: true
});

in $(document).ready(function(){});function.

$(document).ready(function(){});功能上。

Hope this helps.

希望这可以帮助。

回答by Shaiful Islam

You need to initialize datepicker for your input field first

您需要先为输入字段初始化日期选择器

$('#input_filed').datepicker();

Then you need add trigger for button

然后你需要为按钮添加触发器

$('#button').click(function() {
      $('#input_filed').datepicker('show');
});