将 jQuery 日期选择器分配给 div 元素并获取值
Assign jQuery datepicker to div element and get the value
我想在用户单击某些文本时使用 jQuery 显示一个日期选择器,并且在用户 select 日期选择器上的日期之后,我可以从日期选择器中获取值 javascript 变量。我的代码是这样的:
<div id="datepicker-container" style="display: none;">
<div id="select-delivery-date-input"> </div>
</div>
<a id="show-datepicker">Select Delivery Date</a>
<script>
$("#show-datepicker").click(function(){
$("#datepicker-container").show();
});
$('#select-delivery-date-input').datepicker({
dateFormat:'yy-m-d',
minDate: new Date(),
});
</script>
问题是当日期选择器弹出窗口时,当我尝试 select 日期选择器弹出窗口上的日期时,它不会关闭日期选择器弹出窗口。
您可以使用日期选择器的 onSelect
选项:
Called when the datepicker is selected. The function receives the selected date as text and the datepicker instance as parameters.
那你可以用jQuery hide()
.
这里是一个活生生的例子:
$(document).ready(function() {
$("#show-datepicker").click(function(){
$("#datepicker-container").show();
});
$('#select-delivery-date-input').datepicker({
dateFormat:'yy-m-d',
minDate: new Date(),
onSelect: function(selectedDate){
console.log(selectedDate);
$("#datepicker-container").hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<div id="datepicker-container" style="display: none;">
<div id="select-delivery-date-input"> </div>
</div>
<a id="show-datepicker">Select Delivery Date</a>
请在下面找到更改后的代码:
$(document).ready(function() {
var selectedDate;
$("#show-datepicker").click(function(){
$("#datepicker-container").show();
});
$('#select-delivery-date-input').datepicker({
dateFormat:'yy-m-d',
minDate: new Date(),
onSelect: function(date){
selectedDate = date;
alert(selectedDate);//selected date is assigned to date1, you can use date1 anywhere
$("#datepicker-container").hide();
}
}
);
});
我想在用户单击某些文本时使用 jQuery 显示一个日期选择器,并且在用户 select 日期选择器上的日期之后,我可以从日期选择器中获取值 javascript 变量。我的代码是这样的:
<div id="datepicker-container" style="display: none;">
<div id="select-delivery-date-input"> </div>
</div>
<a id="show-datepicker">Select Delivery Date</a>
<script>
$("#show-datepicker").click(function(){
$("#datepicker-container").show();
});
$('#select-delivery-date-input').datepicker({
dateFormat:'yy-m-d',
minDate: new Date(),
});
</script>
问题是当日期选择器弹出窗口时,当我尝试 select 日期选择器弹出窗口上的日期时,它不会关闭日期选择器弹出窗口。
您可以使用日期选择器的 onSelect
选项:
Called when the datepicker is selected. The function receives the selected date as text and the datepicker instance as parameters.
那你可以用jQuery hide()
.
这里是一个活生生的例子:
$(document).ready(function() {
$("#show-datepicker").click(function(){
$("#datepicker-container").show();
});
$('#select-delivery-date-input').datepicker({
dateFormat:'yy-m-d',
minDate: new Date(),
onSelect: function(selectedDate){
console.log(selectedDate);
$("#datepicker-container").hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<div id="datepicker-container" style="display: none;">
<div id="select-delivery-date-input"> </div>
</div>
<a id="show-datepicker">Select Delivery Date</a>
请在下面找到更改后的代码:
$(document).ready(function() {
var selectedDate;
$("#show-datepicker").click(function(){
$("#datepicker-container").show();
});
$('#select-delivery-date-input').datepicker({
dateFormat:'yy-m-d',
minDate: new Date(),
onSelect: function(date){
selectedDate = date;
alert(selectedDate);//selected date is assigned to date1, you can use date1 anywhere
$("#datepicker-container").hide();
}
}
);
});