从 JQUERY 日期选择器获取价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8147108/
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
Getting value from JQUERY datepicker
提问by David Grenier
If I want to display the JQUERY UI datepicker inline by attaching it to a DIV like $("div#someID").datepicker()
- how do I access the chosen date? I assume if it's not bound to an INPUT
then it won't be submitted with the form.
如果我想通过将 JQUERY UI 日期选择器附加到 DIV 来内联显示它$("div#someID").datepicker()
- 如何访问所选日期?我假设如果它没有绑定到 anINPUT
那么它就不会与表单一起提交。
I guess as a followup, if I need to bind it to an INPUT element, is there a way to control the positioning of the datepicker relative to the INPUT element? If I wanted the datepicker to appear above or to the side of the element rather than below it, how would I do that?
我想作为后续,如果我需要将它绑定到 INPUT 元素,有没有办法控制日期选择器相对于 INPUT 元素的定位?如果我希望日期选择器出现在元素的上方或侧面而不是下方,我该怎么做?
Forgive me if this answer is somewhere really obvious.
如果这个答案在某个地方真的很明显,请原谅我。
回答by chapa
If you want to get the date when the user selects it, you can do this:
如果你想在用户选择它时获取日期,你可以这样做:
$("#datepicker").datepicker({
onSelect: function() {
var dateObject = $(this).datepicker('getDate');
}
});
I am not sure about the second part of your question. But, have you tried using style sheets and relative positioning?
我不确定你问题的第二部分。但是,您是否尝试过使用样式表和相对定位?
回答by mu is too short
You can use the getDate
method:
您可以使用以下getDate
方法:
var d = $('div#someID').datepicker('getDate');
That will give you a Date object in d
.
这会给你一个 Date 对象d
。
There aren't any options for positioningthe popup but you might be able to do something with CSS or the beforeShow
event if necessary.
回答by run
$('div#someID').datepicker({
onSelect: function(dateText, inst) { alert(dateText); }
});
you must bind it to input element only
您必须仅将其绑定到输入元素
回答by Bright
You could remove the name attribute of this input, so it won't be submited.
您可以删除此输入的 name 属性,因此不会提交。
To access the value of this controll:
要访问此控件的值:
$("div#someID").datepicker( "getDate" )
and your may have a look at the document in http://jqueryui.com/demos/datepicker/
回答by John Gathogo
You could do it as follows - with validation just to ensure that the datepicker is bound to the element.
您可以按如下方式进行 - 验证只是为了确保日期选择器绑定到元素。
var dt;
if ($("div#someID").is('.hasDatepicker')) {
dt = $("div#someID").datepicker('getDate');
}
回答by evan.z
If your selected elements are not one, you need use this way:
如果你选择的元素不是一个,你需要这样使用:
$('[type="date"]').datepicker();
$('[type="date"]').change(function() {
var date = $(this).datepicker("getDate");
console.log(date);
});
回答by Muhammad Safyan
You can also try this.
你也可以试试这个。
$('#datepickerID').val();
This is the simplest way.
这是最简单的方法。
回答by TeeDeJee
To position the datepicker next to the input field you could use following code.
要将日期选择器放在输入字段旁边,您可以使用以下代码。
$('#datepicker').datepicker({
beforeShow: function(input, inst)
{
inst.dpDiv.css({marginLeft: input.offsetWidth + 'px'});
}
});