javascript 从某事开始获取所有选择/选项列表

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

Get all select/option lists start by something

javascriptdomjsni

提问by superscral

In an HTML page i have severals list.

在 HTML 页面中,我有几个列表。

<select name="salut-1358937506000-OK">
<option selected="" value="OK">OK</option>
<option value="OK">NOK</option>
</select>

<select name="salut-1358937582000-OK">
<option selected="" value="OK">OK</option>
<option value="OK">NOK</option>
</select>
...

In javascript, I want to get all select/option list which started by "salut-". For theses list, i want to compare his name and his selected value.

在javascript中,我想获取以“salut-”开头的所有选择/选项列表。对于论文列表,我想比较他的名字和他选择的值。

I know it is possible in jQuery but can't use jquery, only javascript (JSNI with GWT exactly).

我知道在 jQuery 中是可能的,但不能使用 jquery,只能使用 javascript(JSNI 与 GWT 完全相同)。

Have you an idea?

你有什么想法吗?

Thanks!

谢谢!

回答by Nirvana Tikku

var selects = document.getElementsByTagName('select');
var sel;
var relevantSelects = [];
for(var z=0; z<selects.length; z++){
     sel = selects[z];
     if(sel.name.indexOf('salut-') === 0){
         relevantSelects.push(sel);
     }
}
console.log(relevantSelects);

回答by Justin Ethier

You can use the getElementsByTagNamefunction to get each SELECTname, for example:

您可以使用getElementsByTagName函数来获取每个SELECT名称,例如:

var e = document.getElementsByTagName("select");
for (var i = 0; i < e.length; i++){
  var name = e[i].getAttribute("name");
}

Then you can use the following code to get each OPTIONfor the SELECT, to do any necessary comparisons:

然后你可以使用下面的代码来获得每OPTIONSELECT,做任何必要的比较:

var options = e[i].getElementsByTagName("option")

回答by abdiel vera

var ModalListInputs =document.getElementById("checks")
        var arrayInput = ModalListInputs.getElementsByTagName("input");
        //console.log(arrayInput);

        for(var x=0; x <= arrayInput.length-1; x++){
            if(arrayInput[x].getAttribute("type")=="checkbox" && arrayInput[x].checked ==true){
                console.log(arrayInput[x].value);
            }
        }