jQuery 选择重置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7897760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Chosen reset
提问by P H
I have a bunch of select
elements in a form with which I am using the Jquery Chosen plugin. How can I reset the form? The following does not work:
我select
在使用 Jquery Chosen 插件的表单中有一堆元素。如何重置表格?以下不起作用:
<input type="reset" />
回答by sottenad
You'll need to reset the value of the field, then trigger the liszt:updated
event on the input to get it to update, ive made a fiddle with a working example here.
您需要重置字段的值,然后liszt:updated
在输入上触发事件以使其更新,我在这里做了一个工作示例。
$(".chzn-select").chosen();
$('a').click(function(){
$(".chzn-select").val('').trigger("liszt:updated");
});?
Since the release of chosen v1.0 the trigger is now called 'chosen:updated'. Anyone using this new version needs to trigger the update using
自 selected v1.0 发布以来,触发器现在称为“chosen:updated”。使用此新版本的任何人都需要使用以下命令触发更新
$(".chosen-select").val('').trigger("chosen:updated");
回答by Hyman O'Neill
Since the release of chosen v1.0 the trigger is now called 'chosen:updated'. Anyone using this new version needs to trigger the update using
自 selected v1.0 发布以来,触发器现在称为“chosen:updated”。使用此新版本的任何人都需要使用以下命令触发更新
$(".chosen-select").val('').trigger("chosen:updated");
回答by Edgar O
You could try this:
你可以试试这个:
$('select').chosen('destroy');
$('select').prop("selectedIndex", -1);
$('select').chosen();
回答by buzdykg
in order to have reset working naturally use this:
为了让重置工作自然使用:
$("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');
});
}
回答by Rajesh Kharatmol
$("#Your_id").trigger("chosen:updated");
This worked for me
这对我有用
回答by prograhammer
For a more hassle free approach...assuming your inputs are inside <form>
tags:
对于更轻松的方法......假设您的输入在<form>
标签内:
<form>
<!-- your selects go here and any other input fields -->
<input type="reset" value="Reset">
</form>
This is what I would do:
这就是我会做的:
$("input[type='reset'], button[type='reset']").click(function(e){
setTimeout(function(){ $("select").trigger("chosen:updated"); }, 50);
});
See fiddle here.
请参阅此处的小提琴。
回答by Chris Rosete
None of the previous options work for me. I had to do it like the old school, even using some native javascript, here is the code:
以前的选项都不适合我。我不得不像老派那样做,即使使用一些原生 javascript,这里是代码:
$('#dllSample option').each(function(){
$(this)[0].selected = false;
});
$("#dllSample").trigger("chosen:updated");
回答by Andreas Lyngstad
Sometimes you have to reset the select that chosen is called on.
有时您必须重置所选择的选择。
I do this
我这样做
jQuery.fn.chosen_reset = function(n){
$(this).chosen('destroy');
$(this).prop('selectedIndex', 0);
$(this).chosen(n)
}
And call this function like this, with the options as an argument
并像这样调用这个函数,将选项作为参数
$('select').chosen_reset({width:'369px'});
回答by MHowey
In jQuery something like this should work
在 jQuery 中,这样的事情应该可以工作
<input name="Text1" id="something" type="text">
<input type="reset" id="reset_me"/>
$("#reset_me").click(function() {
$("#something").val("");
});