Javascript 重置所有选择下拉列表

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

Reset all select dropdowns

javascriptjquery

提问by Keith Power

I have have a function that gets called by onbeforeunload. Within I wish to reset all the dropdowns but I cannot find the right method to reset them back to the 0 value.

我有一个被 onbeforeunload 调用的函数。在我希望重置所有下拉菜单中,但我找不到将它们重置回 0 值的正确方法。

<head>
    <script type="text/javascript">
        function displaymessage() {
            document.getElementsByTagName('select').value = 1;
            //or
            document.getElementsByTagName('select').options.length = 0;
        }
    </script>
</head>

<body>
    <form>
        <input type="button" value="Click me!" onclick="displaymessage()" />
    </form>

    <select name="data[Rate][15][12][num_rooms]" id="r15ro12">
        <option value="0">0</option><option value="1">1 Rooms</option>
        <option value="2">2 Rooms</option><option value="3">3 Rooms</option><option value="4">4 Rooms</option>
        <option value="5">5 Rooms</option><option value="6">6 Rooms</option><option value="7">7 Rooms</option>
        <option value="8">8 Rooms</option><option value="9">9 Rooms</option>
    </select>

    <select name="data[Rate][15][12][num_rooms]" id="r15ro12">
        <option value="0">0</option><option value="1">1 Rooms</option>
        <option value="2">2 Rooms</option><option value="3">3 Rooms</option><option value="4">4 Rooms</option>
        <option value="5">5 Rooms</option><option value="6">6 Rooms</option><option value="7">7 Rooms</option>
        <option value="8">8 Rooms</option><option value="9">9 Rooms</option>
    </select>
</body>
</html>

回答by Fabrizio Calderan

function displaymessage() {
    $("select").each(function() { this.selectedIndex = 0 });
}

this selects the first option (not necessarily having value = 0)

这将选择第一个选项(不一定具有值 = 0)

回答by Stefan

Here′s a way to select the first option in all selects using native JavaScript;

select是使用原生 JavaScript在所有s 中选择第一个选项的方法;

var elements = document.getElementsByTagName('select');
for (var i = 0; i < elements.length; i++)
{
    elements[i].selectedIndex = 0;
}

...but jQuery is very handy and makes most things much easier.

...但是 jQuery 非常方便,可以让大多数事情变得更容易。

回答by jabclab

How about something like:

怎么样:

$(window).unload(function () {
    $("select").val("0");
});

回答by nkm

With raw javascript code, we can do the following,

使用原始 javascript 代码,我们可以执行以下操作,

elements = document.getElementsByTagName("select")
for(i=0; i < elements.length ; i++){
 elements[i].selectedIndex= 0;
}

回答by Rory McCrossan

Try this:

尝试这个:

function displaymessage() {
    $("select").val("0");
}

回答by Vid

First of all you should not use the same name/id for the different tag. It should be unique for each tag.

首先,您不应该对不同的标签使用相同的名称/ID。每个标签都应该是唯一的。

In the tag call the function like,

在标签中调用函数,例如,

Then alter ur fun like function displaymessage() { var x= document.getElementsByTagName('select'); for(var i=0;i<2;i++) { document.getElementById(x[i].id).options[0].selected = "0"; } }

然后改变你的乐趣,比如 function displaymessage() { var x= document.getElementsByTagName('select'); for(var i=0;i<2;i++) { document.getElementById(x[i].id).options[0].selected = "0"; } }

回答by h0mayun

Yet Another Way

另一种方式

$('select option:first-child').attr('selected', 'selected');

DEMO

演示

回答by Matt

The jQuery solutions I have seen so far don't take into account to remove the previous selectedattribute if one exists, hence I believe that this extra step is required:

到目前为止,我看到的 jQuery 解决方案没有考虑删除前一个selected属性(如果存在),因此我认为需要执行此额外步骤:

$("#myControlId").find("select").each(function (index) {
        var ctrl=$(this);       
        if (ctrl.children().length > 0) {
            $(ctrl.children()).each(function(index) {
                if (index===0) $(this).attr('selected', 'selected'); 
                else $(this).removeAttr('selected');
            });
        }
});

This solution only keeps one selectedattribute in the first option and removes all the others (if applicable).

此解决方案仅selected在第一个选项中保留一个属性并删除所有其他属性(如果适用)。

回答by Md Shahriar

$('#dropDownListID').val("");

$('#dropDownListID').val("");

or

或者

$('#dropDownListID').val("0");

$('#dropDownListID').val("0");

回答by Aki143S

Try this:

尝试这个:

$( 'select' ).each( function () {
    if ( $( this ).children().length > 0 ) {
        $( $( this ).children()[0] ).attr( 'selected', 'selected' );
    }
} );