我如何知道通过jQuery选择了哪个单选按钮? [英] How can I know which radio button is selected via jQuery?

查看:118
本文介绍了我如何知道通过jQuery选择了哪个单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单选按钮,希望发布选定的值。
如何使用jQuery获得价值?



我可以像这样得到所有的结果:

  $(form:radio)

我知道哪一个被选中?

解决方案

获取选定的值 radioName id为 myForm 的表单项:

  $('input [name = radioName]:checked','#myForm')。val()


$

$('#myForm输入')。on('change',function(){alert($('input [name = radioName]:checked','#myForm').val());});

< script src =https://ajax.googleapis.com/ajax/libs /jquery/2.1.1/jque ry.min.js>< / script>< form id =myForm> < input type =radioname =radioNamevalue =1/> 1< br /> < input type =radioname =radioNamevalue =2/> 2< br /> < input type =radioname =radioNamevalue =3/> 3< br />< / form>

I have two radio buttons and want to post the value of the selected one. How can I get the value with jQuery?

I can get all of them like this:

$("form :radio")

How do I know which one is selected?

解决方案

To get the value of the selected radioName item of a form with id myForm:

$('input[name=radioName]:checked', '#myForm').val()

Here's an example:

$('#myForm input').on('change', function() {
   alert($('input[name=radioName]:checked', '#myForm').val()); 
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
  <input type="radio" name="radioName" value="1" /> 1 <br />
  <input type="radio" name="radioName" value="2" /> 2 <br />
  <input type="radio" name="radioName" value="3" /> 3 <br />
</form>

这篇关于我如何知道通过jQuery选择了哪个单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆