[英]Easiest/cleanest way to get the value of the current <input> element
I want to get the value of the current <input type="radio">
element in its onClick
.我想在其onClick
中获取当前<input type="radio">
元素的值。
I need something like:我需要类似的东西:
<input type="radio" name="myRadio" value="something" onClick="theValueOfThisInput">
I can't use getElementById()
because all my radio buttons have the same id.我不能使用getElementById()
因为我所有的单选按钮都有相同的 id。
I've currently done it by wrapping all the inputs in a <div>
and getting the elements of the <div>
by index, but I won't even bother posting it because I don't think it would be helpful to anyone.我目前已经通过将所有输入包装在<div>
中并通过索引获取<div>
div> 的元素来完成它,但我什至不会费心发布它,因为我认为它对任何人都没有帮助。
Can someone recommend a good way to do this?有人可以推荐一个好方法吗?
Thanks,谢谢,
Have you tried你有没有尝试过
this.value;
? ?
this.value
should certainly work. this.value
肯定可以工作。
Try:尝试:
<input type="radio"
name="myRadio"
value="something"
onclick="alert(this.value)" />
And the 'alert' function can be replaced with whatever function call you wish.并且“警报” function 可以替换为您希望的任何 function 呼叫。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.