如何使用jQuery/JavaScript查找单选按钮何时未选中?

33 浏览
0 Comments

如何使用jQuery/JavaScript查找单选按钮何时未选中?

这个问题已经在这里有了答案

如何知道通过jQuery选中了哪个单选按钮?

我正在尝试使用jquery函数,在检查框被选中时隐藏/显示某些表单元素。同样的函数也应该用于在单选按钮被选中时隐藏/显示表单元素。问题在于单选按钮。我的函数无法判断单选按钮何时未被选中,因此在未被选中时可以隐藏它之前出现的div。我已经在下面显示了代码:


/* the js function */
ShowHideDiv={
init:function(){
    var radcheck = $(".slide");
//if the input element has the class 'slide' then the value of it's 'id' attribute 
//is retrieved and the class which has the same name as the 'id' of the current input
//element is hidden or slided into view - in this case the 'viaMail' class
    radcheck.on("change",function(){
        if($(this).prop('checked')== true) {
            $('body').find("."+$(this).attr('id')).slideDown('slow');
        }
        else {
            $('body').find("."+$(this).attr('id')).slideUp('fast');
        }
    });
}
  }

我已经尝试过为复选框编写的函数,它可以正常工作。但它对于单选按钮不起作用 - div被“滑下”到视图中,但当选择其他单选按钮时,它不会通过“slideUp”消失。真的很感激您的帮助。

admin 更改状态以发布 2023年5月21日
0
0 Comments

你可以使用.is()来检查类似这样的内容。

if( $(this).is(':checked') )

更多信息请参考文档

此外,你可以用$("." + $(this).attr('id'))代替$('body').find("."+$(this).attr('id'))

演示请点击此处

$(function () {
    $('.slide').on("change", function () {
        if ($(this).is(':checked')) {
            $("." + $(this).attr('id')).slideDown('slow');
        } else {
            $("." + $(this).attr('id')).slideUp('fast');
        }
    });
});

0
0 Comments

应该像下面这样

if( $(this).prop('checked', true) )

0