Javascript 如何从jquery datepicker获取选定的日期

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

How to get the selected date from jquery datepicker

javascriptjquerydatepicker

提问by Hansal Mehta

I am using jquery datepicker to show a calendar.Now as per my requirement i want to get the date selected by the user in my jquery variable which i will use in my application but i am not able to get the date .. Here is the code for datepciker

我正在使用 jquery datepicker 来显示日历。现在根据我的要求,我想在我的 jquery 变量中获取用户选择的日期,我将在我的应用程序中使用它,但我无法获取日期..这是日期选择器的代码

<div id="datepicker"></div>

and here i am trying to get the selected code..

在这里我正在尝试获取选定的代码..

$(document).ready(function () {

        $("#datepicker").datepicker({
            onSelect: function (dateText, inst) {
                var date = $(this).val();
                alert(date);
            }
        });

    });

But, I am not able to get the date ..Please help me ..Thanks..

但是,我无法获得日期..请帮助我..谢谢..

回答by akaBase

This should do the trick

这应该可以解决问题

$(function() {
    $("#datepicker").datepicker();
    $("#datepicker").on("change",function(){
        var selected = $(this).val();
        alert(selected);
    });
});

It's basic but here is a jsfiddlewith it alerting the selected date when selected

这是基本的,但这里有一个jsfiddle,它会在选择时提醒选定的日期

update to change the date format

更新以更改日期格式

$(function() {
    $( "#datepicker" ).datepicker({ dateFormat: "yy-mm-dd" });
    $("#datepicker").on("change",function(){
        var selected = $(this).val();
        alert(selected);
    });
});

jsfiddle

提琴手

3rd update

第三次更新

$(function() {
    $("#datepicker").datepicker({ 
        dateFormat: "yy-mm-dd", 
        onSelect: function(){
            var selected = $(this).val();
            alert(selected);
        }
    });
});

I have used a little more of the native markup for datepickerui here try this and see if you get the alert as you are after.

我在这里使用了更多的datepickerui本地标记,试试这个,看看你是否得到了警报。

回答by Ferrakkem Bhuiyan

try this

尝试这个

$('.selector').datepicker({
   onSelect: function(dateText, inst) { ... }
})

you have two elements with the class .datepicker,the selector won't 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

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

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

回答by DanielJRobles

You can use the changeDateevent outlined hereinstead of onSelectand then reference e.dateor e.dates. See the JSON below.

您可以使用此处changeDate概述的事件而不是然后引用或。请参阅下面的 JSON。onSelecte.datee.dates

HTML:

HTML:

<div id='QA'></div>    
<div id='datepicker'></div>

JS:

JS:

<script type="text/javascript">     
    $(function() {
        $('#datepicker').datepicker({
            clearBtn: true,
            todayHighlight: false,
            multidate: true

        }) .on('changeDate', function(e){
            $('#QA').html(JSON.stringify(e));
        });
    });
    /*
    {  
       "type":"changeDate",
       "date":"2015-08-08T07:00:00.000Z",
       "dates":[  
         "2015-08-08T07:00:00.000Z"
       ],
       "timeStamp":1438803681861,
       "jQuery21409071635671425611":true,
       "isTrigger":3,
       "namespace":"",
       "namespace_re":null,
       "target":{  

       },
       "delegateTarget":{  

       },
       "currentTarget":{  

       },
       "handleObj":{  
         "type":"changeDate",
         "origType":"changeDate",
         "guid":52,
         "namespace":""
       }
    }
    */

</script>

回答by Gowri

Try

尝试

$("#datepicker").datepicker({
     onSelect:function(selectedDate)
     {
          alert(selectedDate);
     }
});

OR

或者

$("#datepicker").datepicker({
     onSelect:function (dateText, inst)
     {
          alert(inst);
     }
});