我试图在日期选择器上单击日期时提交表单。我已成功提交,但无法将所选日期传递到表单的隐藏字段。因此,表单已提交,但日期未发送(
我确信这一定很简单,但我对 js 没有经验,所以任何帮助都是值得赞赏的。
代码如下:
<script type="text/javascript">
$(document).ready($(function(){
var $hiddenInput = $('#hiddenFieldID');
$('#datepicker').datepicker({ inline: true })
.bind('click', function(){ $('#myFormID').submit();})
.bind('hiddenFieldID',function(e, hiddenFieldID, $td) { $hiddenInput.val(hiddenFieldID.asString()); });
}));
</script>
<div id="datepicker"></div>
<form id="myFormID" action="index.php" method="GET">
<input type="hidden" name="date" id="hiddenFieldID" value="">
</form>
最佳答案
datepicker
实际上有一些选项可以帮助您完成此任务。请尝试以下操作:
$('#datepicker').datepicker({
inline : true,
altField : '#hiddenFieldID',
onSelect : function(){
$('#myFormID').submit();
}
});
altField: An input element that is to be updated with the selected date from the datepicker. Use the altFormat option to change the format of the date within this field. Leave as blank for no alternate field.
onSelect: Called when the datepicker is selected. The function receives the selected date as text and the datepicker instance as parameters. this refers to the associated input field.
访问完整的 API 以获取与 datepicker
相关的更多选项和事件:http://api.jqueryui.com/datepicker/
关于jQuery UI Datepicker Inline - 单击提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15005857/