【问题标题】:Jquery radio selection from thisjQuery收音机选择从这里
【发布时间】:2020-03-30 06:15:15
【问题描述】:

我正在寻找一种在 jquery 中做类似事情的方法

var my_radio = ("input[name='my_radio']");
my_radio.change(){
    var val = (this":checked").val();
}

我该怎么做? 谢谢

【问题讨论】:

    标签: jquery radio-button


    【解决方案1】:

    你必须编辑这个:

    ("input[name='my_radio']");
    

    对此:

    $("input[name='my_radio']");
    

    如果您有多个无线电输入,那么您也必须进行此更改:

    my_radio.each(function(){
        $(this).on('change', function(){
            if($(this).is(':checked')){
                console.log($(this).val());
            }
        });
    });
    

    示例:

    var my_radio = $('input[type="radio"]');
    my_radio.each(function(){
            $(this).on('change', function(){
                if($(this).is(':checked')){
                    $('span').text($(this).val());
                }
            });
        });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form>
      <input type="radio" name="1" value="1" />
      <input type="radio" name="1" value="2" />
      <input type="radio" name="1" value="3" />
      <input type="radio" name="1" value="4" />
    </form>
    
    <h1>You selected</h1>
    <span></span>

    【讨论】:

    • 是的,对不起,我之前忘记了这个 $ ("input[name='my_radio']")。
    • 顺便说一句比写 my_radio.change(function(){ if($(this).is(':checked')){ console.log($(this).val ()); } });我正在搜索的是如何写 1 次 $(this).is(':checked').val();
    • 不,它比我预期的更容易。它只是 $(this).val() 因为它是检查谁调用了 change()
    • 这将是另一个答案 myRadio.filter(":checked").val();我刚刚在stackoverflow.com/questions/596351/… 上找到了答案
    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 2011-06-04
    • 2012-07-26
    • 2013-03-05
    • 2011-05-21
    • 2012-10-16
    • 2011-11-10
    相关资源
    最近更新 更多