83

我在使用 Jquery Chosen 插件的表单中有一堆select元素。如何重置表格?以下不起作用:

<input type="reset" />
4

10 回答 10

193

您需要重置字段的值,然后liszt:updated在输入上触发事件以使其更新,我在这里做了一个工作示例。

http://jsfiddle.net/VSpa3/3/

$(".chzn-select").chosen();
$('a').click(function(){
    $(".chzn-select").val('').trigger("liszt:updated");
});​

自选择的 v1.0 发布以来,触发器现在称为“选择:更新”。使用此新版本的任何人都需要使用触发更新

$(".chosen-select").val('').trigger("chosen:updated");
于 2011-10-26T01:46:36.710 回答
54

自选择的 v1.0 发布以来,触发器现在称为“选择:更新”。使用此新版本的任何人都需要使用触发更新

$(".chosen-select").val('').trigger("chosen:updated");
于 2013-08-08T17:00:08.760 回答
14

你可以试试这个:

$('select').chosen('destroy');  

$('select').prop("selectedIndex", -1);   
$('select').chosen();
于 2013-08-22T16:08:43.080 回答
11

为了让重置工作自然使用:

$("input[type='reset'], button[type='reset']").click(function(e){
    e.preventDefault();

    var form = $(this).closest('form').get(0);
    form.reset();

    $(form).find('select').each(function(i, v) {
        $(v).trigger('chosen:updated');
    });
}
于 2013-10-28T13:56:17.347 回答
1
$("#Your_id").trigger("chosen:updated");

这对我有用

于 2019-10-09T12:54:13.727 回答
0

对于更轻松的方法...假设您的输入在<form>标签内:

<form>
    <!-- your selects go here and any other input fields -->
    <input type="reset" value="Reset"> 
</form>

这就是我要做的:

$("input[type='reset'], button[type='reset']").click(function(e){
      setTimeout(function(){ $("select").trigger("chosen:updated"); }, 50);
});

在这里看小提琴

于 2014-02-02T23:04:27.417 回答
0

以前的选项都不适合我。我不得不像老派一样做,即使使用一些本机 javascript,这里是代码:

$('#dllSample option').each(function(){
     $(this)[0].selected = false;   
});
$("#dllSample").trigger("chosen:updated");
于 2016-10-06T16:33:50.577 回答
0

这里没有一个答案对我有用。我将选择的选择与多个属性一起使用。正常的提交表单按钮也没有起到作用。所以我只是有一个功能在点击时执行此操作。

//Gets the selected values
var selected = [];
    for (var option of document.getElementById('mySelect').options) {
        if (option.selected) {
            selected.push(option.value);
        }

    }

//Resets the form
document.getElementById('form1').reset();

//Chosen values doesnt get reset, so we do this manually
//Removes the values selected by clicking on the x icon
$link = $('a.search-choice-close');
            var i;
            for (i = 0; i < selected.length; i++) {
                $link[i].click();
            }
于 2020-07-22T07:44:34.303 回答
-1

有时您必须重置调用选择的选择。

我这样做

jQuery.fn.chosen_reset = function(n){
  $(this).chosen('destroy');
  $(this).prop('selectedIndex', 0);
  $(this).chosen(n)
}

并像这样调用这个函数,将选项作为参数

$('select').chosen_reset({width:'369px'});
于 2013-09-04T07:53:47.577 回答
-2

在 jQuery 中,这样的东西应该可以工作

<input name="Text1" id="something" type="text">

<input type="reset" id="reset_me"/>


$("#reset_me").click(function() { 
$("#something").val("");
});
于 2011-10-26T01:34:00.477 回答