Jquery Autocomplete with Django

前端 未结 1 1238
北荒
北荒 2021-02-04 19:28

I\'m trying to make a search of some items with a jquery ui autocomple in a django app. I have looked at this question, and I\'m following just like autocomplete doc. I\'m not i

相关标签:
1条回答
  • 2021-02-04 20:16

    From the autocomplete documentation:

    The label property is displayed in the suggestion menu. The value will be inserted into the input element when a user selects an item.

    And for the select callback:

    Triggered when an item is selected from the menu. The default action is to replace the text field's value with the value of the selected item.

    Canceling [sic] this event prevents the value from being updated, but does not prevent the menu from closing.

    Emphasis mine (spelling mistake theirs). In the jQuery-UI autocomplete source code, we find this:

    if ( false !== self._trigger( "select", event, { item: item } ) ) {
        self.element.val( item.value );
    }
    

    So the widget will set the text input's value to item.value after your select event handler returns. Unless of course (as noted in the documentation above) your event handler returns false. Try adjusting your select handler to be like this:

    select: function( event, ui ) {
        $("#auto_material").val(ui.item.label);
        $("#id_material").val(ui.item.value);
        return false;  // <--------------------- Add this
    }
    

    This is documented behavior so it should be safe.

    0 讨论(0)
提交回复
热议问题