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

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

问题描述

我有两个单选按钮,想发布所选的值.如何使用 jQuery 获取值?

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?

推荐答案

获取 id 为 myForm<的表单的 selected radioName 项的值/代码>:

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

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

这是一个例子:

$('#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天全站免登陆