使用jQuery选择Autoclick Option

时间:2022-08-24 11:12:54

I'm trying to autoselect a product sort filter with jQuery, but I'm having a bit of trouble getting it to work properly.

我正在尝试使用jQuery自动选择产品排序过滤器,但我在使其正常工作时遇到了一些麻烦。

<select class="selectProductSort" id="selectPrductSort1" onchange="">
  <option selected="selected" value="position:asc">Sorter efter</option>
  <option value="price:asc">Pris: laveste først</option>
  <option value="price:desc">Pris: højeste først</option>
  <option value="name:asc">Varenavn: A til Å</option>
  <option value="name:desc">Varenavn: Å til A</option>
  <option value="quantity:desc">Lagervarer først</option>
  <option value="reference:asc">Reference: Laveste først</option>
  <option value="reference:desc">Reference: Højeste først</option>
</select>

I've tried the following so far, but no luck.

到目前为止我已尝试过以下内容,但没有运气。

$('.selectProductSort option[value="price:asc"]').prop('selected', 'selected').change();
$('.selectProductSort option[value="price:asc"]').trigger( 'click' );
$('.selectProductSort option[value="price:asc"]').click();

I tried with $(document).ready and without.

我试过$(document).ready而没有。

The first example sets thedesired filter as selected, but the filter function isn't activated, so the products still are ín the same order.

第一个示例将所需过滤器设置为已选择,但过滤器功能未激活,因此产品仍然是相同的顺序。

When a filter is selected, the page doesn't reload.

选择过滤器后,页面不会重新加载。

Hope you guys can help me out

希望你们能帮助我

https://jsfiddle.net/azc66a0b/

2 个解决方案

#1


2  

Try this : set value of select and then call click() as shown below. But make sure that your click handler must be registered before calling click.

试试这个:设置select的值,然后调用click(),如下所示。但请确保在调用click之前必须注册您的click处理程序。

$('.selectProductSort').val("price:asc").click();

Check below jsfiddle demos before / after calling click

在调用click之前/之后检查下面的jsfiddle演示

JSFIddle with Click handler registered before calling click

在调用click之前注册了JSFIddle和Click处理程序

JSFIddle with Click handler registered after calling click

调用click后注册了JSFIddle和Click处理程序

#2


0  

  $('#selectPrductSort1').val("name:asc");    // Select by value
    text1 = "Reference: Laveste først";
    $("#selectPrductSort1 option:contains(" + text1 + ")").attr('selected', 'selected'); // select by text

#1


2  

Try this : set value of select and then call click() as shown below. But make sure that your click handler must be registered before calling click.

试试这个:设置select的值,然后调用click(),如下所示。但请确保在调用click之前必须注册您的click处理程序。

$('.selectProductSort').val("price:asc").click();

Check below jsfiddle demos before / after calling click

在调用click之前/之后检查下面的jsfiddle演示

JSFIddle with Click handler registered before calling click

在调用click之前注册了JSFIddle和Click处理程序

JSFIddle with Click handler registered after calling click

调用click后注册了JSFIddle和Click处理程序

#2


0  

  $('#selectPrductSort1').val("name:asc");    // Select by value
    text1 = "Reference: Laveste først";
    $("#selectPrductSort1 option:contains(" + text1 + ")").attr('selected', 'selected'); // select by text