代码之家  ›  专栏  ›  技术社区  ›  tbone

如何在表的每一行中使用2个元素(按类)调用函数?

  •  0
  • tbone  · 技术社区  · 16 年前

    我的大致HTML:

    <div id='ComputerID'></div>
    
    <table id='gridComputerApps'>
    <tr>
      <td><div class='licenseOutput'/></td> <td><div class='AppName'>IE6</div></td>
      <td><div class='licenseOutput'/></td> <td><div class='AppName'>Firefox</div></td>
      <td><div class='licenseOutput'/></td> <td><div class='AppName'>SuperApp #2</div></td>
    </tr>
    </table>
    

    因此,使用jQuery,我必须对表中的每一行进行操作。
    对于每一行,我必须将:('#ComputerID').val()、('.AppName').vl()和('.licenseOuput')传递给一个javascript函数,然后该函数使用ComputerID和AppName的值来设置licenseOutput div的文本和颜色。

    所以,我在想一件事 从...开始

        $('#gridComputerApps').children('tr').each(function() {  
            //so, do I now have access to all the ,tr> elements within #gridComputerapps?  
            //now, how do i select the subordinate elements, for each row???  
     });
    

        $('#gridComputerApps tr').each(function() {
            var networkUserID = $("#PrimaryUserNetworkID").text();
            var appName = $(this).find(".AppName").text();
            var licenseOutputCell = $(this).find(".licenseOutput");
            if (appName != '') CheckAppSecForUser(licenseOutputCell, appName, networkUserID);
        });
    

    asp.net用户的重要提示 :

    <asp:TemplateField HeaderText="Application Name" Visible="True" HeaderStyle-Width="15%" ItemStyle-VerticalAlign="Top">
                                <ItemTemplate><div class="xxxAppName"><%#Eval("Application.NormalizedAppName")%></div></ItemTemplate>
                            </asp:TemplateField>
    
    4 回复  |  直到 16 年前
        1
  •  2
  •   richsage    16 年前

    var computerID = $("#ComputerID").text();
    
    $('#gridComputerApps').children('tr').each(function() {
      var appName = $(this).find(".AppName").text();
      var licenseOutput = $(this).find(".licenseOutput").text();
    
      // do something with them
      yourFunc(computerID, appName, licenseOutput);
    });
    

        2
  •  1
  •   Russ Cam    16 年前

    $('#gridComputerApps').children('tr').each(function() {  
        var $tr = $(this);
    
        // all td elements in the row.
        $tr.children('td'); 
    
        // you could use position to get the first td
        // this is the tr element in this case(!)
        var tdFirst = $('td:first', this);
    
        // you could get the next td like so
        // nextAll() will get the next td, next() will only get the next sibling
        tdFirst.nextAll('td');
    
        // get the 3rd td element.
        $tr.children('td').get(2); 
    
        // get the 3rd td element as a jQuery object
        $tr.children('td').eq(2);
    
    });
    
        3
  •  0
  •   brettkelly    16 年前
    $("#gridComputerApps").children("tr").each(function(){
        $(this).children(".licenseOutput").each(function(){
            // do stuff with licenseOutput 
        });
        $(this).children(".AppName").each(function(){
            // do stuff with AppName
        });
    });
    

    $("#gridComputerApps").children("td").each(function(){
        // do stuff with each cell
    });
    

        4
  •  0
  •   Mottie    16 年前

    var cID = $('#ComputerID').text();
    
    $("#gridComputerApps").find("tr").each(function(){
     var license = $(this).find(".licenseOutput");
     var appname = $(this).find(".AppName");
    
     for (i=0;i<license.length;i++){
      yourFunction ( cID, $(license[i]).text(), $(appname[i]).text() );
     }
    });