Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

jquery - Autocomplete Select Redirect

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I change this, it works.

            select: function(event, ui) {   
                location.href="/admin/users/" + ui.item.id + "/edit";
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...