代码之家  ›  专栏  ›  技术社区  ›  Eray Balkanli

如何根据C#中的行级别复选框从HTML表中获取值?

  •  1
  • Eray Balkanli  · 技术社区  · 5 年前

    我有一个包含三个HTML表的aspx页面。HTML中的一个表如下所示:

    <table id='example' >
       <thead>
          <tr>
             <th><INPUT type="checkbox" onchange="checkAll(this)" name="chk[]"/></th>
             <th>SchoolID</th>
             ...
          </tr>
       </thead>
       <tr>
          <td>
             <input type='checkbox' name='name0' />
          </td>
          <td>200116</td>
       </tr>
       <tr>
          <td>
             <input type='checkbox' name='name1' />
          </td>
          <td>200116</td>
       </tr>
       ...
    </table>
    

    enter image description here

    我需要的是拿到 SchoolID 用于最终用户在C#中检查的行。最好的方法是什么?

    我目前正在尝试使用HTMLAgilityPack将我的HTML表作为Datatable,但没有成功。这是第一张桌子,我需要第三张。然而,我不确定这是否是最好的方式。请告知。

    var doc = new HtmlDocument();
    //doc.Load(Request.Url.ToString()); //gives error.
    doc.Load(filepath); //this works
    
    //this is returning me the first table of my page, i need the third table.
    var nodes = doc.DocumentNode.SelectNodes("//table/tr");
    
    var table = new DataTable("exampleDataTable");
    

    编辑:原来我不能使用HTMLAgilityPack,因为我实际上是在用C#创建表,然后发送到前端。使用filepath不起作用,因为默认情况下.aspx文件中没有表。

    EDIT2:Sec,我找到了web.load函数,也许我可以使用Request.Url。

    1 回复  |  直到 5 年前
        1
  •  1
  •   Onur A.    5 年前

    您可以通过提交按钮中的jQuery获取所选行的学校ID click() 听众。

    var tableControl = document.getElementById('mytable');
    $('#myBtn').click(function() {
    
    var arrayOfValues = []
        $('input:checkbox:checked', tableControl).each(function() {
                arrayOfValues.push($(this).closest('tr').find('td:last').text());
            }).get();
        alert(arrayOfValues);
    });
    
    
    $('#chkAll').change(function() {
      $('input:checkbox').not(this).prop('checked', this.checked);
    });
    

    JSFiddle