使用jQuery在表中选择正确的按钮

本文关键字:按钮 选择 jQuery 使用 | 更新日期: 2023-09-27 18:24:42

我有一个HTML表,每行都有一个按钮。我如何编写一个jQuery函数,它将在单击按钮的行中选择列的值。表按预期创建。但我想要按钮点击功能。。。。。有一个名为tablecontent的div。感谢先进的

<script>
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Test.aspx/HTMLCONTROLRECVDATA",
            data: {},
            contentType: "application/json; charset=utf-8",
            dataType: "Json",
            success: function (result) {
                $("#tablecontent").html(result.d);`//add table html is attach with div`}
        });
</script>

C#代码:

public static string HTMLCONTROLRECVDATA()
{
     string connetionString = "Data Source=ARNAB''SQLEXPRESS;Initial Catalog=ABC;Integrated Security=SSPI";
    SqlConnection cnn = new SqlConnection(connetionString);
    SqlCommand cmd = new SqlCommand("usp_mom_HTMLCONTROLRECVDATA", cnn);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cnn.Open();
    SqlDataReader rdr= cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(rdr);
    DataRowCollection drows = dt.Rows;
    StringBuilder HTML = new StringBuilder();
    HTML.AppendLine("<table border='1' id='mainTable'>");
    foreach (DataRow dr in drows)
    {
        HTML.AppendLine("<tr>");
        HTML.AppendFormat("<td>" + dr["AssociateID"] + "</td>'n<td>" + dr["AssociateName"] + "</td><td> <input type='button' class='Button2' value='submit' style='height:20px;width:80px' /></td>'n");
        HTML.AppendLine("</tr>");
    }
    HTML.AppendLine("</table>");
    cnn.Close();
    return HTML.ToString();
}

使用jQuery在表中选择正确的按钮

试试这个:

编辑:为了更好地理解.parent()取父<tr>.next('tr td')取下一个数据单元格

$('button[name=btntbl]').click(function(){
  alert($(this).parent().next('tr td').html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tablebtns">
<tr><th><button name="btntbl">Button Row 1</button></th><td>Test1</td></tr>
<tr><th><button name="btntbl">Button Row 2</button></th><td>Test2</td></tr>
</table>

来自维也纳的问候