1
function updateGrid(data) { // appending the retrieved data(user details) to the grid
        var i = 0;
        var content;
        $("#GridView1").html('');
        $("#GridView1").append("<tr><th>" + "Name" + "</th><th>" + "Gender" + "</th><th>" + "Date of Birth" + "</th><th>" + "Role" + "</th><th>" + "Password" + "</th><th>" + "Email" + "</th><th>" + "Edit" + "</th></tr>");
        while (data[i] != null)
        {
            $("#GridView1").append("<tr><td>" + data[i].name + "</td><td>" + data[i].gender + "</td><td>" + data[i].dob + "</td><td>" + data[i].role + "</td><td>" + data[i].password + "</td><td>" + data[i].email + "</td><td>" + "<button id=btnEdit  onclick='test()'>Edit</button>" + "</td></tr>");// HERE I M DYNAMICALLY CREATING A BUTTON IN THE LAST. WHEN I CLICK IT IT CALLS TEST().HOW CAN I SEND THE DATA IN THIS ROW TO THAT TEST() FUNCTION IN ORDER TO ACCES THEM
            i++;
        }
    }
    function test() {

        alert('edit button is clicked');
        // I NEED TO ACCESS THE ROW'S DATA WHERE THIS EDIT BUTTON IS CLICKED.
    }

这是我的代码。使用 jquery ajax 我从数据库中获取数据并将动态添加到网格中,并且我还在行的末尾创建了一个编辑按钮。我在 buttonclick 上调用方法 test()。如何将当前行的值发送到该 test() 函数或如何在 test() 函数中访问它们...

4

2 回答 2

0

像这样的东西:

$("#GridView1").append("<tr id="+data[i]+"><td>" + data[i].name + "</td><td>" + data[i].gender + "</td><td>" + data[i].dob + "</td><td>" + data[i].role + "</td><td>" + data[i].password + "</td><td>" + data[i].email + "</td><td>" + "<button id=btnEdit  onclick='test("+data[i].Id+")'>Edit</button>" + "</td></tr>");

function test(id) {

    Here use jquery selector to access the <tr> by id, then you can operate with the data in it.
}
于 2013-02-14T13:00:42.910 回答
0

给 Edit 按钮一个类,然后使用 jquery 访问该类及其父类以获取数据

"<button id=btnEdit class='edit'>Edit</button>"

$('.edit').click(function(e){ // pass in e to find the button clicked

    var row = $(e.target).parents('tr'); // or var row = $(e.target).parent().parent();



});

这将最大限度地减少相同 ID 的使用

于 2013-02-14T13:00:50.800 回答