如何通過name獲取value值?
<!DOCTYPE?HTML>
<html>
<head>
<script?type="text/javascript">
function?word(){
????alert(document.getElementsByName("in").value);
}??
</script>
</head>
<body>
<input?name="in"?type="button"?onclick="word()"?value="111"?/>
<input?name="in"?type="button"?onclick="word()"?value="222"?/>
<input?name="in"?type="button"?onclick="word()"?value="333"?/>
</body>
</html>隨意點擊其中一個按鈕,然后獲取它的value值,如何才能做到?。?/p>
2017-04-02
先談談你那個代碼為什么不行吧:
var?arr=document.getElementsByName("in");獲取的是數組,共有
除非你明確指出點擊事件具體指代哪一個數組項,這樣才會有value屬性,即
才會成立。
我貼一下我給出的解決辦法代碼吧:
<!DOCTYPE?HTML> <html> <head> <script?type="text/javascript"> function?word(x){ ????//?var?arr=document.getElementsByName("in"); ????//?alert(arr[0].value); ????alert(x.value); }?? </script> </head> <body> <input?name="in"?type="button"?onclick="word(this)"?value="111"?/> <input?name="in"?type="button"?onclick="word(this)"?value="222"?/> <input?name="in"?type="button"?onclick="word(this)"?value="333"?/> ? </body> </html>這個方法雖然解決了你所說的點擊哪一個就彈出那一個的值,
但是并沒有用到document.getElementsByName()
主要是我沒想到用document.getElementsByName()怎么可以讓瀏覽器定位到我所點擊的標簽。
2018-09-28
<!DOCTYPE?HTML> <html> <head> ????<script?type="text/javascript"> ????????function?word(x){ ????????????var?arr=document.getElementsByName("in"); ????????????alert(arr[x].value); ???????????//?alert(x.value); ????????} ????</script> </head> <body> <input?name="in"?type="button"?onclick="word(0)"?value="111"?/> <input?name="in"?type="button"?onclick="word(1)"?value="222"?/> <input?name="in"?type="button"?onclick="word(2)"?value="333"?/> </body> </html>