I have tried numerous things and nothing seems to be working.

我正在使用jQuery和 Select 的插件.

我try 过的方法:

var select = jQuery('#autoship_option');

select.val(jQuery('options:first', select).val());

jQuery('#autoship_option').val('');

jQuery('#autoship_option').text('');

jQuery('#autoship_option').empty('');

jQuery("#autoship_option option[value='']").attr('selected', true);

It always shows the Active Autoship option once it has been selected. I can't seem to get it to clear the selection.

以下是 Select 框:

<select id="autoship_option" data-placeholder="Choose Option..." 
style="width: 175px;" class="chzn-select">
    <option value=""></option>
    <option value="active">Active Autoship</option>
</select>

是否有人熟悉Selected并能用一个选项清除SelectBox?(future 将有更多 Select .

推荐答案

Setting the select element's value to an empty string is the correct way to do it. However, that only updates the root select element. The custom chosen element has no idea that the root select has been updated.

为了通知chosen select已被修改,您必须触发chosen:updated:

$('#autoship_option').val('').trigger('chosen:updated');

or, if you're not sure that the first option is an empty string, use this:

$('#autoship_option')
    .find('option:first-child').prop('selected', true)
    .end().trigger('chosen:updated');

Read the documentation here (find the section titled Updating Chosen Dynamically).


P.S. Older versions of Chosen use a slightly different event:

$('#autoship_option').val('').trigger('liszt:updated');

Jquery相关问答推荐

jQuery .append() 创建两个元素而不是一个

如何从 jQuery 转到 React.js?

jQuery 1.4.1 中缺少 JSON 字符串化?

jQuery.parseJSON 单引号与双引号

如何在数据表中显示空数据消息

使用 jQuery 的并行异步 Ajax 请求

如何使用 jQuery 清空输入值?

jQuery增量读取AJAX流?

未捕获的类型错误:$.post 不是函数

从 JQuery.ajax 成功数据中解析 JSON

使用 JQuery 更改 :before css Select 器的宽度属性

未捕获的语法错误:无法在文档上执行querySelector

如何使用 jquery 更改 onclick 事件?

用jquery替换锚文本

jQuery '如果 .change() 或 .keyup()'

在 bootstrap 弹出窗口中包含表单?

图片转Base64

从客户端的对象数组中获取最新日期的优雅方法是什么?

将数据发布到 JsonP

使用 Ajax 下载并打开 PDF 文件