Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

javascript - 我怎么知道通过jQuery选择了哪个单选按钮?(How can I know which radio button is selected via jQuery?)

I have two radio buttons and want to post the value of the selected one.

(我有两个单选按钮,想要发布所选一个的值。)

How can I get the value with jQuery?

(如何使用jQuery获得价值?)

I can get all of them like this:

(我可以像这样得到所有这些:)

$("form :radio")

How do I know which one is selected?

(我怎么知道选择了哪一个?)

  ask by juan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(要获取ID为myForm的表单的选定 radioName项目的值:)

$('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> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...