Is there a workaround to trigger an option.click event in IE?

前端 未结 1 888
心在旅途
心在旅途 2020-12-22 00:16

I am using a dropdown to allow the user to sort search results. These results in a table but not all of the sortable criteria are represented by columns. The columns can be

相关标签:
1条回答
  • 2020-12-22 00:47

    To reiterate what T.J. Crowder said in the comments, this is an awkward change that you're making to a user interface. That being said, it's possible to distinguish between clicking on the select element and clicking on an option element if the select doesn't have the multiple attribute set. Try out the following code in Internet Explorer:

    $(document).ready(function ()
    {
      $("#sortSelect").click(function (event) {
        if (event.offsetY > this.offsetHeight)
          $(this.options[this.selectedIndex]).click();
      });
      $("#sortSelect option").click(function (event)
      {
        alert('clicked option '+this.parentNode.selectedIndex);
      });
    });
    

    It works because when you click on an option element, the click event fires on the select element. By detecting that you've clicked outside the bounds of the select element checking event.offsetY > this.offsetHeight, you can use jQuery to trigger the click event of the currently selected option element.

    0 讨论(0)
提交回复
热议问题