选择的jQuery下拉列表=在Safari中选择无效

问题描述 投票:15回答:2

我有一个带有几个标签的订阅表单。您可以此形式向后和向前单击。

对于下拉菜单,我使用jquery来预先选择一个先前的选择,因此人们不必在向后单击时重新回答这个特定问题。

此脚本在Chrome和Firefox上运行良好,但是对于Safari则完全不起作用。不知何故我无法使它正常工作。

您能看一下并告诉我我做错了吗?

脚本:

if (repeat_visit !== '') {
    $('#repeatVisit').find('option').each(function(){
        if ($(this).val() === repeat_visit) {
            $(this).attr('selected','selected');
        }
    });
}

属性设置为选项,因此它看起来像:是

我可以看到确实发生了,但是在页面加载后添加选定属性时,Safari似乎不会更改页面。因此,我需要以某种方式触发此更改事件。

jquery html drop-down-menu safari html-select
2个回答
40
投票

我找到了答案!

您可以使用.prop('selected',true)代替attr('selected','selected');


0
投票

我只是使用.prepend()更改DOM元素,以将选择的选项添加到select内的第一个元素。

        selectElement.children[i].setAttribute("selected", true);
        selectElement.prepend(selectElement.children[i]);
© www.soinside.com 2019 - 2024. All rights reserved.