如何使用 JQuery 侦听 RadioGroup 选定值的更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4091554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:38:54  来源:igfitidea点击:

How do I listen for changes to a RadioGroup's selected value using JQuery?

jqueryradio-buttononchangeradio-group

提问by Justin

I need to register a handler for a group of radio buttons. I'm using JQuery and hoped that its .changemethod would accomplish this. However I have not experienced the desired behavtheitroad.

我需要为一组单选按钮注册一个处理程序。我正在使用 JQuery 并希望它的.change方法能够实现这一点。但是,我还没有经历过想要的行为。

Here is a sample snippet I've written. Sadly, the "radioValueChanged" is only called on the initial load. Selecting either true / false does not trigger the handler.

这是我写的一个示例片段。遗憾的是,“radioValueChanged”仅在初始加载时被调用。选择 true / false 不会触发处理程序。

<html>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>

<form id="myForm">
    <div id="Question1Wrapper">
        <div>
            <input type="radio" name="controlQuestion" id="valueFalse" value="0" />
            <label for="valueFalse">
                False</label>
        </div>
        <div>
            <input type="radio" name="controlQuestion" id="valueTrue" value="1" />
            <label for="valueTrue">
                True</label>
        </div>
    </div>
    <div id="Question2Wrapper">
        <div>
            <label for="optionalTextBox">
                This is only visible when the above is true</label>
            <input type="text" name="optionalTextBox" id="optionalTextBox" value="" />
        </div>
    </div>

    <script type="text/javascript">
        jQuery(document).ready(function ()
        {
            $("#controlQuestion").change(radioValueChanged('controlQuestion'));
        })

        function radioValueChanged(radioName)
        {
            radioValue = $('input[name=' + radioName + ']:checked', '#myForm').val();

            alert(radioValue);

            if(radioValue == 'undefined' || radioValue == "0")
            {
                $('#Question2Wrapper:visible').hide();
            }
            else
            {
                $('#Question2Wrapper:visible').show();
            }
        } 
    </script>
</form>

回答by Quintin Robinson

There are a few issues here.

这里有几个问题。

  1. You are immediately running radioValueChanged('controlQuestion')upon script execution because that is a method call and not a function assignment.

  2. The selector $("#controlQuestion")is wrong, you don't have any elements with id of controlQuestion.

  3. The radioValueChangedmethod is not properly handling values as they would be passed to a jQuery event handler.

  1. 您会radioValueChanged('controlQuestion')在脚本执行时立即运行,因为那是方法调用而不是函数分配。

  2. 选择器$("#controlQuestion")是错误的,您没有任何 id 为 的元素controlQuestion

  3. radioValueChanged方法未正确处理值,因为它们将传递给 jQuery 事件处理程序。

You could try something like the following:

您可以尝试以下操作:

jQuery(document).ready(function ()
    {
        $("input[name='controlQuestion']").change(radioValueChanged);
    })

    function radioValueChanged()
    {
        radioValue = $(this).val();

        alert(radioValue);

        if($(this).is(":checked") && radioValue == "0")
        {
            $('#Question2Wrapper').hide();
        }
        else
        {
            $('#Question2Wrapper').show();
        }
    } 

In all honesty I'm not sure if that is the actual logic you are looking for with the if statement, but hopefully this will provide a basis for you to correct the current code.

老实说,我不确定这是否是您使用 if 语句寻找的实际逻辑,但希望这将为您更正当前代码提供基础。

回答by John Strickler

$('#Question2Wrapper:visible').show();

Take out the :visible, this will only select it if the div is already showing, in effect it will never be shown if it is hidden.

取出:visible,这只会在 div 已经显示时选择它,实际上,如果它被隐藏,它将永远不会显示。

$('#Question2Wrapper').show();

I digress, I think Quintin hits most of the points. There are a few issues going on here.

我离题了,我认为昆汀击中了大部分分数。这里有几个问题。