本文翻译自:jQuery get value of selected radio button

The problem statement is simple. 问题陈述很简单。 I need to see if user has selected a radio button from a radio group. 我需要查看用户是否已从广播组中选择了一个单选按钮。 Every radio button in the group share same id. 组中的每个单选按钮共享相同的ID。

The problem is that I don't have control on how the form is generated. 问题是我无法控制表单的生成方式。 Here is the sample code of how a radio button control code looks like: 这是单选按钮控制代码的示例代码:

<input type="radio" name='s_2_1_6_0' value='Mail copy to my bill to address' id = "InvCopyRadio" onchange = 'SWESubmitForm(document.SWEForm2_0,s_4,"","1-DPWJJF")' style="height:20;width:25" tabindex=1997 >

In addition to this when a radio button is selected it doesn't add a "checked" attribute to the control just text checked (I guess just the property checked without a value) . 除此之外,当选择单选按钮时,它不会向控件添加“ checked”属性,而只是对文本进行检查 (我猜只是对属性进行了检查,没有值)Below is how a selected radio control looks like 以下是选定的无线电控件的外观

<input type="radio" checked name='s_2_1_6_0' value='Mail copy to my bill to address' id = "InvCopyRadio" onchange = 'SWESubmitForm(document.SWEForm2_0,s_4,"","1-DPWJJF")' style="height:20;width:25" tabindex=1997 >

Can anybody help me with jQuery code that can help me to get the value of checked radio button? 有人可以帮助我使用可以帮助我获取选中的单选按钮值的jQuery代码吗?


#1楼

参考:https://stackoom.com/question/aB3w/jQuery获取所选单选按钮的值


#2楼

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

This will return, checked radio button value. 这将返回选中的单选按钮值。

if($("input[type='radio'].radioBtnClass").is(':checked')) {var card_type = $("input[type='radio'].radioBtnClass:checked").val();alert(card_type);
}

#3楼

Just use. 随便使用。

$('input[name=name_of_your_radiobutton]:checked').val();

So easy it is. 很容易。


#4楼

I am not a javascript person, but I found here for searching this problem. 我不是一个JavaScript的人,但是我在这里找到了搜索此问题的人。 For who google it and find here, I am hoping that this helps some. 对于谁用谷歌搜索并在这里找到,我希望这会有所帮助。 So, as in question if we have a list of radio buttons: 因此,就像我们是否有单选按钮列表一样:

<div class="list"><input type="radio" name="b1" value="1"><input type="radio" name="b2" value="2" checked="checked"><input type="radio" name="b3" value="3">
</div>

I can find which one selected with this selector: 我可以找到使用此选择器选择的一个:

$('.list input[type="radio"]:checked:first').val();

Even if there is no element selected, I still don't get undefined error. 即使没有选择任何元素,我也不会得到未定义的错误。 So, you don't have to write extra if statement before taking element's value. 因此,在获取元素值之前,您不必编写额外的if语句。

Here is very basic jsfiddle example . 这是非常基本的jsfiddle示例 。


#5楼

The best way to explain this simple topic is by giving simple example and reference link:- 解释此简单主题的最佳方法是给出简单的示例和参考链接:-

In the following example we have two radio buttons. 在下面的示例中,我们有两个单选按钮。 If the user selects any radio button, we will display the selected radio button value in a alert box. 如果用户选择任何单选按钮,我们将在警报框中显示选定的单选按钮值。

Html: HTML:

<form id="Demo">
<input type="radio" name="Gender" value="Male" /> Male <br />
<input type="radio" name="Gender" value="Female" /> Female<br />
<input type="radio" name="Gender" value="others" /> others <br />
</form>

jquery:- jQuery的:-

 $(document).ready(function(){$('#Demo input').on('change', function() {alert($('input[name="Gender"]:checked', '#Demo').val());});});

#6楼

<div id="subscriptions">Company Suscription: <input type="radio" name="subsrad" value="1">Customer Subscription:<input type="radio" name="subsrad" value="2">Manully Set:<input type="radio" name="subsrad" value="MANUAL">NO Subscription:<input type="radio" name="subsrad" value="0"></div>

and handle jquery for alert as for th e value set / Changed through div id: 并处理jquery以获取有关值设置/通过div ID更改的警报:

$("#subscriptions input") // select the radio by its id.on('change', function(){ // bind a function to the change eventalert($('input[name="subsrad"]:checked', '#subscriptions').val());});

it is so easy....:-} 这很容易。...:-}

jQuery获取所选单选按钮的值相关推荐

  1. html简单获取多选框的,jquery获取复选框的值的简单实例

    JS获取复选框被选中的值 0 1 2 3 4 5 6 7 JS代码 对checkbox的其他几个操作 1. 全选 2. 取消全选 3. 选中所有奇数 4. 反选 5. 获得选中的所有值 js代码 $( ...

  2. js,jquery获取复选框checkbox被选中的值

    转载:https://blog.csdn.net/qq_35792598/article/details/76646983 <!DOCTYPE html PUBLIC "-//W3C/ ...

  3. jquery获取复选框checkbox被选中的值

    获取复选框基本操作 html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "h ...

  4. 用jQuery如何获取复选框的值

    获取复选框的值 <div><input type="checkbox" name="" id="suibian">& ...

  5. js+jQuery获取全选并用ajax进行批量删除

    今天在处理后台的时候做了个小小的ajax处理批量删除的功能,给大家分享下. 1.在Repater绑定出放入单选 <input id="chkContentId" name=& ...

  6. 如何通过js获取html文本框中的值,js与jquery获取input输入框中的值实例讲解

    如何用javascript获取input输入框中的值,js/jq通过name.id.class获取input输入框中的value 先准备一段 HTML 一.jquery获取input文本框中的值 通过 ...

  7. 微信小程序获取多选框选中值和选中值对应的id

    微信小程序开发交流qq群   173683895    承接微信小程序开发.扫码加微信. 官方文档中只有获取多选框的值的方法,但是我需要获取选中的值同时还要获取选中值对应的id,但是又不能操作DOM获 ...

  8. html取 输入框中的值,jquery获取input输入框中的值

    如何用javascript获取input输入框中的值,js/jq通过name.id.class获取input输入框中的value 先准备一段 HTML 一.jquery获取input文本框中的值 通过 ...

  9. jquery 获取当前元素的索引值

    jquery 获取当前元素的索引值 $("#lisa > li").mouseover(function(){ alert($("#lisa > li&quo ...

最新文章

  1. springboot不会运行gc_SpringBoot项目深度优化和Jvm调优
  2. 前端学习(2536) request和response
  3. oracle创建表不在对应表空间,oracle创建表空间
  4. Shell 条件表达式的正则匹配
  5. 题目:求1 + 2!+ 3!+ ... + 20!的和
  6. android 模拟器 超时,Appcelerator Studio超时等待Android模拟器启动
  7. matlab构造arma模型,ARMA模型构建及MATLAB实现
  8. LVDS学习笔记之lvds_transceiver设计及仿真
  9. 计算机不同用户共享文件,如何为不同账户设置共享文件的不同访问权限
  10. 计算机视觉SIFT算法详解
  11. 2020年国内优秀原创IT技术书都在这了
  12. 自动化(二)使用Python打开网页
  13. 每日简报 5月16日简报新鲜事 每天一分钟 了解新鲜事
  14. how to define the RASIC in a team
  15. PDPS软件:工装夹具机械装置运动操作的创建方法
  16. 高斯模糊磨皮——ps
  17. Android10的GPU呈现模式分析在哪?
  18. http 301、302、304、400、405、415状态码解释
  19. CycleGAN中欺骗相反域的鉴别器是什么意思
  20. 边缘计算 — 与云计算厂商

热门文章

  1. VScode 搭建 django 开发环境(Win Python3.71 django1.11.11)
  2. 区块链100问41-60问
  3. 2017-05-24
  4. Python - 列表解析式/生成器表达式
  5. OpenGL shader 中关于顶点坐标值的思考
  6. genlist -s 192.168.21.\*
  7. HTML5 学习(1) -- 介绍
  8. 漏洞扫描工具Vulmap
  9. ELK收集tomcat日志
  10. 查看Oracle当前用户下的信息