JQuery Datepicker 获取选定日期

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

JQuery Datepicker Get Selected Date

ajaxjquery

提问by RyK

I am currently working with 2 separate JQuery datepickers that I have modified the format to be able to pass through to store in a backend MySQL db.

我目前正在使用 2 个单独的 JQuery 日期选择器,我已经修改了格式,以便能够通过存储在后端 MySQL 数据库中。

<script>
$(function() {
$( ".datepicker" ).datepicker({
        inline: true,
        showOtherMonths: true,
        dateFormat: 'yy-mm-dd',
        dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],  
    });
});
</script>


My date pickers are called here

我的日期选择器在这里被调用

<input type="text" class='datepicker' name="datepicker1" />
<input type="text" class='datepicker' name="datepicker2" />

I have been trying to get the selected date to pass to php via ajax. I have tried using the getDate function but have not had much luck.

我一直在尝试通过 ajax 将选定的日期传递给 php。我曾尝试使用 getDate 函数,但运气不佳。

$(".datepicker").datepicker( 'getDate' );

$(".datepicker").datepicker('getDate');

回答by krishgopinath

Since you have two elements with the class .datepicker, the selector wont know which element to choose from. So, you'll have to specify the name of the input you're trying to get the date from.

由于您有两个带有 class 的元素.datepicker,因此选择器不知道要从哪个元素中进行选择。因此,您必须指定要从中获取日期的输入的名称。

first = $(".datepicker[name=datepicker1]").datepicker('getDate');
second = $(".datepicker[name=datepicker2]").datepicker('getDate');

Demo : http://jsfiddle.net/hungerpain/TWmcD/

演示:http: //jsfiddle.net/hungerpain/TWmcD/

Note :

笔记 :

Won't val()be easier than using getDate? You could format your code date in mysql itself and save work for you in the clientside.

不会val()比使用更容易getDate吗?您可以在 mysql 本身中格式化您的代码日期,并在客户端为您保存工作。

first = $(".datepicker[name=datepicker1]").val();
second = $(".datepicker[name=datepicker2]").val();

回答by Arun P Johny

$(".datepicker").datepicker( 'getDate' )will return only the date value from the first element of $(".datepicker")selector, in this case it might be datepicker1.

$(".datepicker").datepicker( 'getDate' )将只返回$(".datepicker")选择器第一个元素的日期值,在这种情况下它可能是datepicker1.

In order to get values of both these input fields you need to use

为了获得这两个输入字段的值,您需要使用

var dfs = $('.datepicker');
var f1 = dfs.filter('[name="datepicker1"]').datepicker( 'getDate' );
var f2 = dfs.filter('[name="datepicker2"]').datepicker( 'getDate' );

Demo: Fiddle

演示:小提琴

回答by skparwal

When you select the date you can call an another Ajax function to sent the selected date to PHP.

当您选择日期时,您可以调用另一个 Ajax 函数将所选日期发送到 PHP。

onClose: function( selectedDate ) {
    $.ajax({url: 'test.php', data: {date: selectedDate}});
}

回答by skparwal

use jquery plug-in to get a datepicker

使用jquery插件获取日期选择器

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

also see jsfiddle

另见jsfiddle