jQuery UI autocomplete select event not working with mouse click

前端 未结 9 779
星月不相逢
星月不相逢 2020-12-28 15:09

I have a list of links, and I have this search box #reportname. When the user types in the search box, autocomplete will show the text of the links in a list.



        
相关标签:
9条回答
  • 2020-12-28 15:31

    I tested it in all version of IE (inlcuding 9) and always ended up with an empty input-control after I selected the item using the mouse. This caused some headaches. I even went down to the source code of jQuery UI to see what happens there but didn’t find any hints either.

    We can do this by setting a timeout, which internally queues an event in the javascript-engine of IE. Because it is guaranteed, that this timeout-event will be queued after the focus event (this has already been triggered before by IE itself).

    select: function (event, ui) {
        var label = ui.item.label;
        var value = ui.item.value;
        $this = $(this);
        setTimeout(function () {
            $('#txtBoxRole').val(value);
        }, 1);
    },
    
    0 讨论(0)
  • 2020-12-28 15:38

    I was facing a similar problem. I wanted to submit the form when the user clicked on an option. But the form got submitted even before the value of the input could be set. Hence on the server side the controller got a null value.

    I solved it using a modified version of William Niu's answer.

    Check this post - https://stackoverflow.com/a/19781850/1565521

    0 讨论(0)
  • 2020-12-28 15:39

    After inspecting the code in the developer tools console, I noticed there were two list items added. I removed the pairing <li></li> from my response code and oh yeah, the links worked

    I also added this function as the click event:

     $("#main-search").result(function ()
        {
            $("#main-search").val("redirecting...."), window.location.href = $("#main-search").attr("href").match(/page=([0-9]+)/)[1];
        })
    

    This works and you can test it here: Search for the term dress -->

    0 讨论(0)
  • 2020-12-28 15:44

    To your 2nd question: "How can I differentiate between select events that are triggered by keyboard with the ones triggered by mouse?"

    The event object in the jQuery UI events would include a .originalEvent, the original event it wrapped. It could have been wrapped multiple times though, such as in the case of Autocomplete widget. So, you need to trace up the tree to get the original event object, then you can check for the event type:

    $("#reportname").autocomplete({
        select: function(event, ui) {
            var origEvent = event;
            while (origEvent.originalEvent !== undefined)
                origEvent = origEvent.originalEvent;
            if (origEvent.type == 'keydown')
                $("#reportfind").click();
        },
        ...
    });
    
    0 讨论(0)
  • 2020-12-28 15:47

    Thanks to @William Niu and firebug, I found that the select event parameter 'ui' contains the complete selected value: ui.item.value. So instead of depending on jquery UI to change the text of the textbox, which didn't happen if the user clicks with mouse, I just pick up the selected value from 'ui':

    $("#reportname").autocomplete({
        select: function (event, ui) {
            var reportname = ui.item.value
            var thelinks = $('a.large:contains("' + reportname + '")').filter(
                function (i) { return (this.text === reportname) })
            window.location = thelinks[0].href
        };
    })
    
    0 讨论(0)
  • 2020-12-28 15:49

    I had the same issue, mouse click was not selecting the item which was clicked.My code was supposed to make an ajax call to fetch the data as per the selection item from autocomplete source.

    Previous code: mouse click not working.

     select: function(event, ui) {
              event.preventDefault();
              for(i= 0; i< customer.length; i++)
              if(document.getElementById('inputBox').value == customer[i].name)
              {
    
              $.ajax({
              call
              })
    

    Changed code :mouse click working

    select: function(event, ui) {
              // event.preventDefault();
              for(i= 0; i< customer.length; i++)
              // if(document.getElementById('inputBox').value == customer[i].fields.name)
              if(ui.item.value == customer[i].name)
              {
              $.ajax({
              call
              })
    
    0 讨论(0)
提交回复
热议问题