简体   繁体   中英

How to select all inputs by name?

I know you can use this to get all elements with the name 'target':

document.getElementsByName('target')

But what if I want to select only inputs with that name?

在纯JavaScript中,您可以使用document.querySelectorAll

document.querySelectorAll('input[name="target"]');

你可以做:

var inputs = document.querySelectorAll("input[name=target]");

Since you can catch forms by name you can do

<form name="outerTarget">    
    <input type="button" value="test" name="target">
</form>
<script>
document.forms['outerTarget'].target;
</script>

MDN: https://developer.mozilla.org/en-US/docs/Web/API/Element.name#Example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM