简体   繁体   English

获取当前值的最简单/最干净的方法<input>元素

[英]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.

相关问题 将 Enum 放入键值对的最简单方法 - Easiest way to get Enum in to Key Value pair 在 Android 中获取星期几的最简单方法是什么? - What is the easiest way to get the current day of the week in Android? 以 YYYYMMDD 格式获取当前日期的最简单方法是什么? - What is the easiest way to get the current date in YYYYMMDD format? 如果在jsoup中定义了元素,分配给变量的最干净方法是? - Cleanest way to assign to a variable if an element in jsoup is defined? 从输入字符串获取令牌的最简单方法是什么? - What is the easiest way to get tokens from input String? 在一项的HashMap中获得值的最简单方法是什么? - What is the easiest way to get the Value in a HashMap of one item? 无法获取“输入”元素的值 - Cannot get the value of an “input” element 基于字符串输入将值插入列表的最快/最干净的方法是什么? - what is the fastest/cleanest way to insert values into lists based on a string input? 根据业务逻辑验证用户输入的最简洁方法是什么? - What is the cleanest way to validate user input depending on business logic? 从Maven获取JUnit测试用例中最简单的文件的方法 - Cleanest way to get a File in a JUnit test case from Maven
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM