I am trying to redirect the page on the selection of an item on autocomplete. I am using JSON to populate the autocomplete. How can I get it to redirect to a URL with the ID from the JSON? I tried using the select function but that doesn't seem to work. It is not even going to a URL when selecting an item. Any help would be greatly appreciated!
<script type="text/javascript">
$(function() {
$('#user').autocomplete({
minLength: 2,
source: '<%= admin_users_path(:json) %>',
focus: function(event, ui) {
$('#user').val(ui.item.first_name + " " + ui.item.last_name);
return false;
},
select: function(event, ui) {
location.href="/admin/users/" + val(ui.item.id) + "/edit";
}
})
.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.data( "ui-autocomplete-item", item )
.append( "<a>" + item.first_name + " " + item.last_name + "</a>" )
.appendTo( ul );
};
});
</script>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…