使用JQuery查找集合单选按钮组名称 [英] use JQuery to find the collective radio button group name

查看:56
本文介绍了使用JQuery查找集合单选按钮组名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了很多jQuery问题,因为我正在尝试使用它相当好的旧Javascript,正如我在之前的问题中提到的那样,我目前不得不扩展一个非常古老的ASP.NET站点,一个数据库生成的前端,是一个应用程序的庞然大物,它需要完全重写 - 但是我被告知要添加它而不是重新开发它

I've asked a lot of jQuery questions recently as I'm trying to use it rather good old Javascript, as I mentioned in previous questions, "I'm currently having to extend an very old ASP.NET site which has a database generated front end and is a behemoth of an application, it needs completely rewriting - however I've been told to add to it than redevelop it "

现在我是什么我正在做的是,一旦后端将表格呈现给我希望循环通过表格的tr元素的界面,在tr的td元素之一中有两个单选按钮。我需要确定单选按钮组的名称,因为我没有定义它们,系统使用GUID或其他东西?

Now what I'm doing is once the backend is rendering a table to the interface I wish to loop through the tr elements of the table, within one of the td elements of the tr there are two radio buttons. I need to determine the name of the radio button group as I don't define them, the systems does using a GUID or something?

这是表格的例子。 ..

This is the table for example...

<table id="tableID">
    <tr>     
        <td class="qCol">     
        <!-- Label here -->     
        </td>      
        <td class="qCo2">     
        <!-- img here -->     
        <!-- and a text box -->     
        </td>      
        <td class="qCo3">     
        <!-- select menu here -->     
        </td>      
        <td class="qCo4">     
        <!-- radio buttons here -->     
        </td>      
        <td class="qCo5">     
        <!-- select menu here -->     
        </td>     
        <td class="qCo6">     
        <!-- hidden validation image here -->     
        </td> 
    <tr> 
</table> 

好的,我正在遍历表格,同时将一个单元格的innerHTML切换为另一个并隐藏了一些行,我将添加功能以便稍后显示,这里是jQuery:

Okay, I'm looping through the table and at the same time switching the innerHTML of one cell to another and hiding some of the rows, I'll be adding functionality to show these later, here's the jQuery:

$('#tableID tr').each(function (i) {

        /*switch select and validation and clear */
        $(this).children('td.qCol').html($(this).children('td.aCol5').html());
        $(this).children('td.aCol5').html($(this).children('td.vCol'.html(""));

        /* hide all but the first row*/
        if (i >= 1) {
            $(this).hide();
        }

现在我正在尝试确定其名称属性将在类.qCo4的单元格中的单选按钮,但是我没有运气,因为以下返回错误并且是未定义...

now I'm trying to determine the name attribute of the radio buttons that will be in the cell with class .qCo4, however I'm having no luck as the following returns an error and is "undefined"...

/* get the radio button name and check if either are selected*/

// check something exists...
if ($('input:radio', this).attr('name')) {

    var thisRadioName = $(this).closest("input:radio").attr('name').val();
    alert(thisRadioName);

}

$ this tr 元素,所以我应该使用child而不是最接近?如果这没有意义,我可以更好地扩展和解释。

$this is the tr element, so should I be using child rather than closest? If this makes no sense I can expand and explain better.

推荐答案

你应该使用 find() children() nearest()在树上,而find在树下查找元素

you should use find() or children(). closest() goes up the tree, while find looks for elements down the tree

var thisRadioName = $(this).find("input:radio").attr('name');

您还可以使用':checked'来检查是否有任何收音机被检查

you can use also ':checked' to check if any radio is checked

var checkedRadio = $(this).find("input:radio:checked")

如果您确定要查找的元素是元素的直接子元素,则应使用子元素,否则使用find()。 (如果你修改了html,使用find会保护你不重构代码,就像添加一个包装div一样,因为其他原因,如styiling)

You should use children if you are absolutely sure that the elements you are looking for are direct child of the element, otherwise use find(). (using finds protect you from refactoring code if you modify the html, like adding a wrapping div for other reasons like styiling)

看看这个小提琴: http://jsfiddle.net/nicolapeluchetti/Evq6y/

这篇关于使用JQuery查找集合单选按钮组名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆