jQuery UI Datepicker Inline - submiting form on click

我怕爱的太早我们不能终老 提交于 2019-12-05 03:29:45

问题


I am trying to submit form when date is clicked on datepicker. I have managed to submit, but cannot pass chosen date to hidden field of the form. So, form submits, but date is not sent (

I am sure it must be simple to do, but I am not experienced with js, so any help is appreciated.

Here's the code:

<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>

回答1:


The datepicker actually has a few options to help you with this. Try the following:

$('#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.

Visit the full API for more options and events related to datepicker: http://api.jqueryui.com/datepicker/




回答2:


I fought with this same issue. In my case, my began and ended inside a table. Moving the form outside the table fixed the issue.

Gene



来源:https://stackoverflow.com/questions/15005857/jquery-ui-datepicker-inline-submiting-form-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!