jQuery-UI Autocomplete submitting form onclick of item from dropdown list

≯℡__Kan透↙ 提交于 2019-12-01 06:02:12

问题


I'm using the jQueryUI autocomplete() function and I can't figure out how to have my form submit when an item is selected.

I think the issue is with the select: event but I'm new with jQueryUI and can't figure out how to make this work.

Here's my code which works fine otherwise:

            <script type="text/javascript">
            $(document).ready(function() {
                $(function() {
                    $( "#search_box" ).autocomplete({
                        source: function(request, response) {
                            $.ajax({ url: "<?php echo site_url('autocomplete/suggestions'); ?>",
                            data: { term: $("#search_box").val()},
                            dataType: "json",
                            type: "POST",
                        success: function(data){
                            response(data);
                        },
                        select: function (event, ui) {
                                $(event.target).val(ui.item);
                                $('#search_form').submit();
                                return false;
                            }
                        }); 
                    },          
                    minLength: 1 
                    });
                });
            }); 
            </script>

Any assistance would be greatly appreciated!


回答1:


Andrew was correct, see the fiddle he mentioned. If you switch the part with "ui.item" to "ui.item.value" the select: function() now works perfectly.




回答2:


Yes it works

Except if you expect to get the value in your server-side script in the page called by action...

For now, no way to find why auto-submit of the form after selection by mouse or by keyboard, and the $(event.target).val(ui.item.value) line, don't deliver the value in $_POST array




回答3:


Yeah Alice is also right. You just need to add this line in your onclick function:

 $('#search_form').submit();

where search_form is the id of your form.



来源:https://stackoverflow.com/questions/6619500/jquery-ui-autocomplete-submitting-form-onclick-of-item-from-dropdown-list

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