简体   繁体   English

jQuery基于样式类名称查找所有单选输入的方法

[英]jQuery way to find all radio inputs based on style class name

I'm using the below javascript function to get all the radio buttons on my page and clear them. 我正在使用下面的javascript函数获取页面上的所有单选按钮并清除它们。 The problem is I don't want to clear all of them. 问题是我不想清除所有这些。 Is there a way to just clear a group of them? 有没有办法清除其中的一组? I think the only way I can logically group them (since they're dynamically generated by server side mappings) is to give all the ones I want unchecked a common style class name and search that way. 我认为,我可以对其进行逻辑分组的唯一方法(因为它们是由服务器端映射动态生成的)是为所有我想要取消选中的对象提供一个通用样式类名称,并以此方式进行搜索。 Any ideas on how to modify the below such that it only finds certain radio buttons who have a certain style class name? 关于如何修改以下内容以便仅查找具有特定样式类名称的某些单选按钮的任何想法? align-top is my styleClass name. align-top是我的styleClass名称。

<script type="text/javascript">
       function clearOtherSelections(selRow){
           var radios = jQuery('td > input[type=radio]');
           radios.attr('checked', false);
           selRow.checked = 'checked';                   
       }
</script>

When I used the revised code below, var radios = jQuery('.align-top'); 当我使用下面的修订代码时, var radios = jQuery('.align-top'); alerts that all the radios are unchecked which is untrue. 提醒所有无线电未选中,这是不正确的。 When I use my old way var radios = jQuery('td > input[type=radio]'); 当我使用旧方法时, var radios = jQuery('td > input[type=radio]'); it shows one radio checked an the others not, which is correct. 它显示一个收音机检查了一个,而其他收音机没有检查,这是正确的。 Why doesn't jQuery('.align-top'); 为什么不jQuery('.align-top'); show an accurate state of the radio checked poperty? 显示无线电检查的人口的准确状态?

revised code: 修改后的代码:

function clearOtherSelections(selRow){
                        //var radios = jQuery('td > input[type=radio]');
                        var radios = jQuery('.align-top');                        
                        for( i = 0; i < radios.length; i++ ) {
                            if( radios[i].checked ) {
                                alert('checked')
                                radios.attr('checked', false);
                            }else{
                                alert('unchecked')
                            }
                        }

                        selRow.checked = 'checked';                    
                    }

I'd suggest: 我建议:

function clearOtherSelections(selRow){
    var radios = jQuery('td > input.specificClassName:radio');
    radios.prop('checked', false);
    selRow.checked = 'checked';                   
}

You're setting the checked property , rather than the attribute , so use the prop() method, rather than attr() . 您是在设置checked 属性 ,而不是属性 ,因此请使用prop()方法,而不是attr()

If the class will only be applied to the specific input elements you want to style, you might be able to use, simply: 如果该类仅应用于您要设置样式的特定input元素,则可以简单地使用:

var radios = jQuery('.specificClassName');

or: 要么:

var radios = jQuery('input.specificClassName');

References: 参考文献:

Try this: 尝试这个:

<script type="text/javascript">
       function clearOtherSelections(selRow){
           var radios = jQuery('td > input[type=radio].someclass');
           radios.prop('checked', false);
           selRow.checked = 'checked';                   
       }
</script>

You can use :radio selector, which comes along with jQuery library, and it is more faster as compare to input[type=radio]. 您可以使用jQuery库随附的:radio选择器,它比input [type = radio]更快。

$("td > input:radio.yourClass") // Gives you all the radio button which has yourClass

jQuery : radio selector jQuery: 单选按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM