jQuery UI autocomplete with item and id

前端 未结 11 1800
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 00:32

I have the following script which works with a 1 dimensional array. Is it possible to get this to work with a 2 dimensional array? Then whichever item is selected, by clic

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 00:51

    This can be done without the use of hidden field. You have to take benefit of the JQuerys ability to make custom attributes on run time.

    ('#selector').autocomplete({
        source: url,
        select: function (event, ui) {
            $("#txtAllowSearch").val(ui.item.label); // display the selected text
            $("#txtAllowSearch").attr('item_id',ui.item.value); // save selected id to hidden input
        }
    });
    
    $('#button').click(function() {
        alert($("#txtAllowSearch").attr('item_id')); // get the id from the hidden input
    }); 
    

提交回复
热议问题