javascript 单击按钮时重置下拉值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9358974/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:23:39  来源:igfitidea点击:

Reset Dropdown value on button click

javascriptjqueryhtml

提问by LovingMVC

I am calling a jquery function to clear my all input fields on button click, but my requirement is when you click on clear all button it should reset my dropdown value to 1 index not to 0 index.

我正在调用一个 jquery 函数来清除我点击按钮时的所有输入字段,但我的要求是当你点击清除所有按钮时,它应该将我的下拉值重置为 1 索引而不是 0 索引。

please let me know how i can do this.

请让我知道我该怎么做。

function ClearAll() {
    $("#BasicCaseStatus").val(1); // This is my dropdown
}

This is my button.

这是我的按钮。

<a href="#"  id="SearchClear" onclick="ClearAll()" class="clearallLink" >Clear All</a>

回答by Darin Dimitrov

Try this:

试试这个:

$('select option:eq(1)').prop('selected', 'selected')???;?

It will select the second <option>in the dropdownlist. Or if you wanted to do this for a particular dropdown:

它将选择<option>下拉列表中的第二个。或者,如果您想为特定下拉菜单执行此操作:

$('#BasicCaseStatus option:eq(1)').prop('selected', 'selected')???;?

回答by GregL

First you want to deselect all options, then filter to the second option and select it.

首先您要取消选择所有选项,然后过滤到第二个选项并选择它。

$('a.clearallLink').on('click', function() {
    $('#BasicCaseStatus option').prop('selected', false).eq(1).prop('selected', true);
});

Note this works with jQuery 1.7 and onwards, I believe.

请注意,我相信这适用于 jQuery 1.7 及更高版本。

回答by ParaMeterz

function dropdown_clear()
    {
        $('#dropdownlist').html(null);
    }

Here, I had created a function for clear the dropdown list on button click. Now, juzt copy paste the complete function and juzt call it on the button click.

在这里,我创建了一个功能,用于在单击按钮时清除下拉列表。现在,直接复制粘贴完整的功能,然后在单击按钮时直接调用它。

回答by Mattia Larentis

Js:

JS:

$('span').click(function(){ $("#BasicCaseStatus").val(1); });?

$('span').click(function(){ $("#BasicCaseStatus").val(1); });?

Html:

网址:

<select id="BasicCaseStatus">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<span>Clear All</span>

?Link: http://jsfiddle.net/J5EE7/

?链接:http: //jsfiddle.net/J5EE7/

回答by codef0rmer

None of the above answers would work If the default selected option is not the first one. The best way to achieve this is to wrap a select dropdown in <form>tag and call the reset() function.

如果默认选择的选项不是第一个,则上述答案均无效。实现此目的的最佳方法是在<form>标签中包装一个选择下拉列表并调用 reset() 函数。

<form id='frm'>
<select id="BasicCaseStatus">
<option>1</option>
<option selected>2</option>
<option>3</option>
</select>

<span>Clear All1 | </span>
</form>

$('span').click(function(){  
  $('form')[0].reset()
});

Demo: http://jsfiddle.net/KS6Yh/

演示:http: //jsfiddle.net/KS6Yh/

回答by Nerijus Gedrimas

  1. Don't write javascript code in HTML file (Remove onclick)
  2. Use this js code to select second value from the list:
  1. 不要在 HTML 文件中编写 javascript 代码(删除 onclick)
  2. 使用此 js 代码从列表中选择第二个值:

$('#BasicCaseStatus').prop('selectedIndex', 1);

$('#BasicCaseStatus').prop('selectedIndex', 1);

A working example

一个工作示例

回答by ravichand

be it in jquery or javascript :-

$('#idname').html(null); or 
document.getElementById('#idname').selectedIndex = 0;  or 
document.getElementById('#idname').selectedIndex = -1;  

the options hasn't worked for me in dynamic dropdown list. so, i have tried

$('#idname').val("")       

try oneself for good results.

回答by madhu

Use this for JQuery $("#droplist").empty();

将此用于 JQuery $("#droplist").empty();

回答by franzisk

I think the most elegant solution is this

我认为最优雅的解决方案是这个

$('#BasicCaseStatus option:eq(1)').prop("selected",true);