jquery autocomplete get selected item text

后端 未结 4 579
傲寒
傲寒 2021-02-07 04:22

I am wondering how to grab the selected item\'s text value on jquery autocomplete.

I have initialised jquery as following :

$(document).ready(function ()         


        
相关标签:
4条回答
  • 2021-02-07 04:57
    // list of clients
            $( "#client" ).autocomplete({
            source: "lib/getClientList.php",
            minLength: 2,
            select: function(event, ui) {
                alert(ui.item.value);
                 }
            })
    

    ;

    The ui.item.value is the reference that jquery utilizes to assign a value to the input

    0 讨论(0)
  • 2021-02-07 04:58

    The ui parameter has an item property with the selected text

    function AutoCompleteSelectHandler(event, ui)
    {               
        var selectedObj = ui.item;              
        alert(selectedObj.value);
    }
    

    source: http://jqueryui.com/demos/autocomplete/#event-select go to tab "Events" and then event "Select"

    0 讨论(0)
  • 2021-02-07 05:04

    You just grab the value from the input in the same way you would if the user had typed it in themself:

    $('input#autocomplete').val()
    
    0 讨论(0)
  • 2021-02-07 05:12

    You don't need to apply an anonymous function as a wrap, you can directly pass the function ref.

    $(document).ready(function (){
       $("input#autocomplete").autocomplete({
            source: postcodelist,
            select: AutoCompleteSelectHandler
        });
    });
    

    Within that method, you can either access this or event.target to get the current value, both values are referencing the input element:

    function AutoCompleteSelectHandler(event, ui) {
        alert( $(event.target).val() );
        alert( $(this).val() );
    
        // alert( this.value );
    }
    
    0 讨论(0)
提交回复
热议问题