问题
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.
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?
Forgive me if this answer is somewhere really obvious.
回答1:
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?
回答2:
You can use the getDate
method:
var d = $('div#someID').datepicker('getDate');
That will give you a Date object in d
.
There aren't any options for positioning the popup but you might be able to do something with CSS or the beforeShow
event if necessary.
回答3:
$('div#someID').datepicker({
onSelect: function(dateText, inst) { alert(dateText); }
});
you must bind it to input element only
回答4:
You could remove the name attribute of this input, so it won't be submited.
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/
回答5:
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');
}
回答6:
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);
});
回答7:
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'});
}
});
来源:https://stackoverflow.com/questions/8147108/getting-value-from-jquery-datepicker