问题
There seem to be (at least) two ways to make a JQuery datepicker submit when a date is selected. Firstly,
onSelect : function (dateText, inst) { $('#myFormID').submit(); }
from
Post form on select with JQuery Datepick
or
Datepicker Submit form when select date
The drawback with this is that it embeds the ID of the form, so if you copy the code of an existing datepicker and overlook this, it won't work; if you don't overlook it, changing the ID is still one more thing to do.
So, this version
onSelect : function() { $(this).parent('form').submit(); }
looks better as it can be copied without the need for amendment. However, if the datepicker is associated with a textbox, and the textbox is inside a table, then it won't work and this line is required instead
onSelect : function() { $(this).parents('form').submit(); }
(see jquery datepicker won't submit inside a table)
The difference - just in case it's not obvious - is 'parents' not 'parent'.
I've now tried using 'parents' for all my datepickers, and they all seem to work fine - but rarely is it the recommended way to submit-on-select (e.g. https://forum.jquery.com/topic/datepicker-onselect-submit-date for a non-SO example). Does anyone know a circumstance in which
onSelect : function() { $(this).parents('form').submit(); }
would not submit-on-select, or is it in fact the best and most readily reusable option?
来源:https://stackoverflow.com/questions/35041664/most-reusable-code-for-submit-on-select-with-jquery-datepicker