35

在我的应用程序中,我需要一个无线电组,每当检查无线电按钮时,就会发出警报,以便我可以将其价值发布到jquery的Ajax帖子中。

你能帮我请我如何在jQuery中做到这一点吗?

4

6 回答 6

57

尝试这样的事情:

$(function(){
  $('input[type="radio"]').click(function(){
    if ($(this).is(':checked'))
    {
      alert($(this).val());
    }
  });
});

如果你给你的单选按钮一个类,那么你可以$('input[type="radio"]')$('.someclass').

于 2010-04-28T17:35:23.597 回答
14

2017 年更新:嘿。这是一个可怕的答案。不要使用它。在过去,这种类型的 jQuery 使用很常见。它可能在当时有效。只需阅读它,意识到它很糟糕,然后继续(或否决或其他)对今天的 jQuery 更好的其他答案之一。


$("input[type=radio]").change(function(){
    alert( $("input[type=radio][name="+ this.name + "]").val() );
});
于 2010-04-28T17:36:40.833 回答
5

HTML 代码:

<input type="radio" name="theName" value="1" id="option-1">
<input type="radio" name="theName" value="2">
<input type="radio" name="theName" value="3">

Javascript代码:

$(document).ready(function(){
    $('input[name="theName"]').change(function(){
        if($('#option-1').prop('checked')){
            alert('Option 1 is checked!');
        }else{
            alert('Option 1 is unchecked!');
        }
    });
});

在名为“theName”的多个收音机中,检测选项 1 何时被选中或未选中。适用于所有情况:点击控制、使用键盘、使用操纵杆、自动更改其他动态功能的值等。

于 2017-02-25T00:40:01.573 回答
0

您可以简单地使用changeJQuery 的方法来获取当前单选的值,并使用以下代码进行检查:

$(document).on('change', '[type="radio"]', function() {
    var currentlyValue = $(this).val(); // Get the radio checked value
        
    alert('Currently value: '+currentlyValue); // Show a alert with the current value
});

您可以更改所需'[type="radio"]'的类或 ID 的选择器。

于 2018-04-04T22:27:30.467 回答
0
$("#expires1").click(function(){
     if (this.checked)
        alert("testing....");
});
于 2018-08-22T12:30:00.930 回答
0
$(document).on('change','.radio-button', function(){

    const radio = $(this);

    if (radio.is(':checked')) {
      console.log(radio)
    }

});
于 2019-11-25T09:01:47.833 回答