如何获取已选中的单选按钮的值,jquery

发布于 2024-07-26 08:08:05 字数 200 浏览 2 评论 0原文

我们能否获取单选按钮的值,仅获取那些已选中的值, 因为我必须实现一个有很多单选按钮的表单,所以我怎样才能获取所有已检查的值 我要做的是

我有13门科目,比如:物理、数学、生物...... 学生可以选择任意数量的科目,每个科目都有两个单选按钮,一个是 A,另一个是 As,

那么我如何获取已选中的单选按钮的值? 使用jquery

can we get the values of the radio buttons , only those which has been checked ,
cause i have to implement a form in which there are many radio buttons , so how can i get all the values which has been checked
what i have to do is

i have 13 subjects like : physics , math , biology .....
and the students can choose any number of subjects , and each subject will have two radio buttons one is A and the another is As ,

so how can i get the values of the radio buttons which has been checked?
using jquery

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-08-02 08:08:05

这将循环遍历文档中已检查的所有单选元素:

$('input:radio:checked').each(function() {
    var value = $(this).val();
    // do something with radio or value
});

如果您想获取特定单选组的检查值,您可以这样做:

var value = $('input:radio[name=myradios]:checked').val();

因此,如果您的 HTML 如下所示:

<input type='radio' name='myradios' value='1'>
<input type='radio' name='myradios' value='2' checked='checked'>
<input type='radio' name='myradios' value='3'>

value将是 2

This would loop through all the radio elements in the document that have been checked:

$('input:radio:checked').each(function() {
    var value = $(this).val();
    // do something with radio or value
});

If you wanted to get the checked value of a particular radio group, you would do:

var value = $('input:radio[name=myradios]:checked').val();

So if your HTML was like this:

<input type='radio' name='myradios' value='1'>
<input type='radio' name='myradios' value='2' checked='checked'>
<input type='radio' name='myradios' value='3'>

value would be 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文