Javascript 如何使用javascript按值选择输入元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8926378/
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
How to select an input element by value using javascript?
提问by Pieter888
I've seen it's jquery equivalent:
我已经看到它与 jquery 等效:
$('input[value="something"]');
But how do you select it using pure javascript (no jQuery).
但是你如何使用纯 javascript(没有 jQuery)来选择它。
Thanks for all the responses so far but I'm sure if it is working correctly, I need to change the value of the input into something else. I though I could do this by
感谢到目前为止的所有回复,但我确定它是否正常工作,我需要将输入的值更改为其他值。我虽然我可以做到这一点
<enter snippet to select element here>.value = "someOtherValue";
But it appears to be not that easy. Any ideas.
但这似乎并不那么容易。有任何想法吗。
回答by
with ie6-ie7-ie8
与 ie6-ie7-ie8
function getInputsByValue(value)
{
var allInputs = document.getElementsByTagName("input");
var results = [];
for(var x=0;x<allInputs.length;x++)
if(allInputs[x].value == value)
results.push(allInputs[x]);
return results;
}
with modern browsers ie9+ (? not sure for ie9 actually) :
使用现代浏览器 ie9+(?实际上不确定 ie9):
document.querySelectorAll("input[value=something]");
回答by jabclab
You can use document.querySelectorAll()
on modern browsers (https://developer.mozilla.org/En/DOM/Document.querySelectorAll), e.g.
您可以document.querySelectorAll()
在现代浏览器(https://developer.mozilla.org/En/DOM/Document.querySelectorAll)上使用,例如
var byValue = document.querySelectorAll('input[value="something"]');
For older browsers you'll have to iterate over the input
s and check the value, e.g.
对于较旧的浏览器,您必须遍历input
s 并检查值,例如
var inputs = document.getElementsByTagName("input"),
i,
len,
byVal = [],
value = "something";
for (i = 0, len = inputs.length; i < len; i++) {
if (inputs[i].value === value) {
byVal.push(inputs[i]);
}
}
回答by Esailija
var elems = [].filter.call( document.getElementsByTagName("input"), function( input ) {
return input.value === "something";
});
回答by Chris
Something like this works:
像这样的工作:
function getCheckboxByValue(v) {
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
if(inputs[i].type == "checkbox" && inputs[i].value == v) {
return inputs[i];
}
}
return false;
}
(function testCheckbox() {
getCheckboxByValue("1").checked = true;
})();
Using jQuery would be much better, though.
不过,使用 jQuery 会好得多。
回答by JamesHalsall
Something like this should work...
这样的事情应该工作......
for(i in document.getElementsByTagName('input')) {
if(i.value == 'desiredValue') {
return i;
}
}
Edit: This will return an array of all matches
编辑:这将返回所有匹配项的数组
var matches = [];
for(i in document.getElementsByTagName('input')) {
if(i.value == 'desiredValue') {
matches.push(i);
}
}
回答by Grant Miller
This might be overboard in most cases, but an alternative method would be to use document.evaluate
with an XPath expression to select the input field by value:
在大多数情况下,这可能有点过分,但另一种方法是使用document.evaluate
XPath 表达式按值选择输入字段:
let result = document.evaluate(
'//input[@value="something"]',
document,
null,
XPathResult.ANY_TYPE,
null
).iterateNext();
result.value = 'someOtherValue';
Note:This method is not supported by Internet Explorer.
注意:Internet Explorer 不支持此方法。
Otherwise, use one of the other mentioned methods for better browser compatibility and speed.
否则,请使用其他提到的方法之一以获得更好的浏览器兼容性和速度。
回答by Erik ?sterling
If you want all elements by ID you can use
如果你想要所有元素的 ID,你可以使用
function getElementsById(elementID){
var elementCollection = new Array();
var allElements = document.getElementsByTagName("*");
for(i = 0; i < allElements.length; i++){
if(allElements[i].id == elementID){
elementCollection.push(allElements[i]);
}
}
return elementCollection;
}