如何通过使用 jquery 引用其类名来从单击的单选按钮中获取值?

匿名的

我有 3 个具有相同类的单选按钮,当我单击特定的单选按钮时,我需要的是,然后该单选按钮的值应该通过在脚本下方引用单选按钮的类名来提醒,这不符合我的要求。

if($("input[type='radio'].radioBtn").prop('checked')) {
    var btntype = $("input[type='radio'].radioBtn:checked").val();
    alert(btntype);
}
<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>

  <input type="radio" class="radioBtn" name="numbers" value="1" />1<br/>
  <input type="radio" class="radioBtn" name="numbers" value="2" />2<br/>
  <input type="radio" class="radioBtn" name="numbers" value="3" />3<br/>

</body>
</html>

大肠杆菌

click每次有人单击输入值时,您都应该使用事件来触发您的函数。您可以在jQuery API Doc 中阅读有关事件的更多信息

$(document).ready(function() {
  $("#inputs").on('click', 'input[type="radio"]', function() {
    alert($(this).val()); // this points to the clicked element
  });
});
<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
  <div id="inputs">
    <input type="radio" class="radioBtn" name="numbers" value="1" />1<br/>
    <input type="radio" class="radioBtn" name="numbers" value="2" />2<br/>
    <input type="radio" class="radioBtn" name="numbers" value="3" />3<br/>
  </div>
</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何获取在jQuery中单击其相应按钮的表中的行的记录

如何通过使用jQuery自动对html中的行求和?

通过使用jQuery单击标签来取消选中选中的单选按钮

如何通过使用jQuery检查父元素的类

如何通过使用javascript(不允许使用jQuery)获取类名来更改HTML样式?

如何通过<a>使用Jquery在div内获取firs子代的值

单击单选按钮时,如何使用jquery从表单获取值?

如何通过使用react在jquery中添加props函数

通过单击jquery中的按钮来提醒src

如何通过单击JQuery中的类来更改两个元素的CSS?

如何通过使用jQuery包装来对HTML元素进行分组

如何使用jQuery通过单击功能删除类

jQuery使用类名获取值

如何在jQuery中从同一个类名div获取值

如何通过使用jQuery更改引导按钮样式

如何通过单击jQuery中的按钮来更改图像

如何通过解析类信息使用jQuery创建类

如何通过单击使用jquery更改音频src?

如何通过使用jQuery在cshtml文件中调用函数?

类的单选按钮单击了jquery

如何通过使用jQuery从div中的多个类中获取指定的类名称?

如何使用jquery来获取所单击的每个按钮中的数据属性

如何通过使用javascript或jquery获取日期范围之间的天数

如何使用jQuery中的类获取选定单选按钮和复选框的值?

如何使用jquery生成动态输入并通过id获取值

如何使用jquery从不同的类中获取值

如何使用 jquery 从滑块中获取值

如何从单选按钮输入字段中获取值?JS , jQuery

如何通过单击其 div 来选择单选按钮?