javascript jQuery datepicker - 将日期存储在变量中并打印出来

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

jQuery datepicker - storing the date in a variable and printing it

javascriptjquerydatepicker

提问by user2791728

Since I am new to jQuery I have a simple question to the pro-users.

因为我是 jQuery 的新手,所以我有一个简单的问题要问专业用户。

  1. How do I print out on the screen the date from the datepicker?
  2. How to assign the date from datepicker to a variable so I can use it later for using that date's with mySQL database manipulation.
  1. 如何在屏幕上打印日期选择器中的日期?
  2. 如何将 datepicker 中的日期分配给一个变量,以便我以后可以使用它来使用该日期与 mySQL 数据库操作。

Any help appreciated.

任何帮助表示赞赏。

$(function() {
    $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: "yy/mm/dd",
        showAnim: 'clip',
        onClose: function(selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
        }
    });

    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1, maxDate: 0,
        dateFormat: "yy/mm/dd",
        showAnim: 'clip',
        onClose: function(selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});

回答by BenM

$( "#from" ).datepicker("getDate"); //to get the date

var date;

date = $( "#from" ).datepicker("getDate"); //to store it in a variable.

The API is useful if you need to do more with your datepicker.

如果您需要使用日期选择器执行更多操作,该 API 非常有用。

http://api.jqueryui.com/datepicker/#method-getDate

http://api.jqueryui.com/datepicker/#method-getDate

回答by Feedthe

you can use onSelect method to assign selected value to some variable. You can post that value to your controller and save it to database.

您可以使用 onSelect 方法将选定的值分配给某个变量。您可以将该值发布到您的控制器并将其保存到数据库中。

$(function() {

    var selectedDate = "";

    $("#from").datepicker( {
        onSelect: function(date) {
            alert(date);
            selectedDate = date;
        },
        selectWeek: true,
        inline: true,
        startDate: '01/01/2000',
        firstDay: 1
    });
});

回答by Negi Rox

Use

利用

getDate

获取日期

it Returns the current date for the date picker or null if no date has been selected.

它返回日期选择器的当前日期,如果没有选择日期,则返回 null。

var fromDate= $( "#from" ).datepicker( "getDate" );
var toDate= $( "#to" ).datepicker( "getDate" );