jquery获取单选按钮组的值

jquery获取单选按钮组的值,jquery,button,radio,Jquery,Button,Radio,这是我的html代码: <form name="dform"> <fieldset> <input type="radio" class="yes" name="g1" value="1"/> Yes<br/> <input type="radio" class="no" name="g1" value="0"/> No </fieldset> <fieldset> <inpu

这是我的html代码:

<form name="dform">
<fieldset>
    <input type="radio" class="yes" name="g1" value="1"/> Yes<br/>
    <input type="radio" class="no" name="g1" value="0"/> No
</fieldset>
    <fieldset>
   <input type="radio" class="yes" name="g2" value="1"/> Yes<br/>
        <input type="radio" class="no" name="g2" value="0"/> No
    </fieldset>
    <fieldset>
    <input type="radio" class="yes" name="g3" value="1"/> Yes<br/>
        <input type="radio" class="no" name="g3" value="0"/> No
    </fieldset>
    <input type="reset" class="reset" value="RESET"/><BR/>
</form>
<div id="result">N/A</div>

但它不起作用。我应该修复什么?

一些语法错误和score\u yes不应该用jQuery包装

function scoring(){
        var score_yes = 0;
        var score_no = 0;
        $(".yes:checked").each(function(){
            score_yes += parseInt(this.value);
        });

        $(".no:checked").each(function(){
            score_no += parseInt(this.value);
        });

        if(score_yes==3){
            $("#result").html('LOW');
        }else if(score_yes==2){
            $("#result").html('MEDIUM');
        }else if(score_no==3){
            $("#result").html('HIGH');
        }else if(score_no==2){
            $("#result").html('HIGH');
        }else{
            $("#result").html('N/A');
        }
    }
$(document).ready(function(){
    $(".yes").change(function(){
        scoring();
    });
 });

有很多问题,从几个地方的额外括号到在jquery对象中包装int(
$(例如,score_yes)
)到绑定
。是:选中了
(我相信您想要
。是,
,因为这两个问题都会改变
的结果。你也可以做一些简化。我相信这就是你想要的:


您可以做得更简单:

function scoring() {
    var score_yes = $("input.yes:checked").length;
    var score_no = $("input.no:checked").length;

    if (score_no >= 2)
        return 'HIGH';
    else if (score_yes == 3)
        return 'LOW';
    else if (score_yes == 2)
        return 'MEDIUM';
    else
        return 'N/A';
}

$(document).ready(function(){
    $('input[type="radio"]').change(function(){
        scoring();
    });
});

还有一个括号:

$(".yes:checked").each(function() ) {
应该是

$(".yes:checked").each(function(){

我个人的建议是:

/* caching the radio inputs, to save time later
   (assuming there's a static collection): 
*/
var radios = $('input[type="radio"]');

// setting up the event-handler:
radios.change(function(){
    // filtering for how many '.yes' inputs are checked:
    var yes = radios.filter('.yes:checked').length,
    // filtering for how many '.no' inputs are checked:
        no = radios.filter('.no:checked').length;

    // setting the text of the '#result' element:        
    $('#result').text(function(){
        /* assuming a binary decision, then only, at most,
           half of the total number of elements can be checked, if they are
           all choices have been made:
        */
        if (yes + no === (radios.length)/2) {
            /* if 'yes' scores more than 1 (so either 2 or 3) we return 'Low',
               otherwise 'High':
            */
            return yes > 1 ? 'Low' : 'High';
        }
        // some are unchecked, therefore we return 'N/A':
        else {
            return 'N/A';
        }
    });
// triggering the 'change' event on DOM-ready, in case of default-checked radio inputs:
}).change();

参考资料:


您的代码中有多个语法错误,其中有多余的
{
)不需要它们。此外,您的主要问题是更改侦听器的选择器,
$(“.yes:checked”)
,只有已选中的选定单选按钮。您可以使用
$([input[type='radio']“”
作为您的选择器。您的完整示例可以在下面找到

$(document).ready(function () {
    $("input[type='radio']").change(function () {
        var score_yes = 0;
        var score_no = 0;
        $(".yes:checked").each(function () {
            score_yes += parseInt($(this).val());
        });

        $(".no:checked").each(function () {
            score_no += parseInt($(this).val());
        });

        if (score_yes === 3) {
            $("#result").html('LOW');
        } else if (score_yes === 2) {
            $("#result").html('MEDIUM');
        } else if (score_no === 3) {
            $("#result").html('HIGH');
        } else if (score_no === 2) {
            $("#result").html('HIGH');
        } else {
            $("#result").html('N/A');
        }
        });
    });

什么不起作用?你收到错误消息了吗?@Bartdude没有任何错误消息,但我能看到的只是结果“N/A”,尽管我单击了任何单选按钮。@AGF,其中至少有两种情况会导致控制台错误。
$(".yes:checked").each(function(){
/* caching the radio inputs, to save time later
   (assuming there's a static collection): 
*/
var radios = $('input[type="radio"]');

// setting up the event-handler:
radios.change(function(){
    // filtering for how many '.yes' inputs are checked:
    var yes = radios.filter('.yes:checked').length,
    // filtering for how many '.no' inputs are checked:
        no = radios.filter('.no:checked').length;

    // setting the text of the '#result' element:        
    $('#result').text(function(){
        /* assuming a binary decision, then only, at most,
           half of the total number of elements can be checked, if they are
           all choices have been made:
        */
        if (yes + no === (radios.length)/2) {
            /* if 'yes' scores more than 1 (so either 2 or 3) we return 'Low',
               otherwise 'High':
            */
            return yes > 1 ? 'Low' : 'High';
        }
        // some are unchecked, therefore we return 'N/A':
        else {
            return 'N/A';
        }
    });
// triggering the 'change' event on DOM-ready, in case of default-checked radio inputs:
}).change();
$(document).ready(function () {
    $("input[type='radio']").change(function () {
        var score_yes = 0;
        var score_no = 0;
        $(".yes:checked").each(function () {
            score_yes += parseInt($(this).val());
        });

        $(".no:checked").each(function () {
            score_no += parseInt($(this).val());
        });

        if (score_yes === 3) {
            $("#result").html('LOW');
        } else if (score_yes === 2) {
            $("#result").html('MEDIUM');
        } else if (score_no === 3) {
            $("#result").html('HIGH');
        } else if (score_no === 2) {
            $("#result").html('HIGH');
        } else {
            $("#result").html('N/A');
        }
        });
    });