jQuery UI Datepicker Inline - submiting form on cl

2019-03-31 09:49发布

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>

2条回答
该账号已被封号
2楼-- · 2019-03-31 10:14

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/

查看更多
戒情不戒烟
3楼-- · 2019-03-31 10:18

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

登录 后发表回答