简体   繁体   中英

Anchor tag onclick function in javascript code

I have made a dynamic table using jquery, Now I want to edit that table with a link, I write onclick function like that:

var array = $.parseJSON(arr);
debugger
for (var i = 0; i < array.length; i++) {
    var row = "<tr>"
    + "<td>" + array[i].ID + "</td>"
    + "<td>" + array[i].Name + "</td>"
    + "<td>" + array[i].FatherName + "</td>"
    + "<td>" + array[i].RollNo + "</td>"
    + "<td>" + array[i].Age + "</td>"
    + "<td>" + array[i].Phone + "</td>"
    + "<td>" + array[i].Address + "</td>"
    + "<td><a href='#' onclick='check()'>Edit</a></td>"
    + "<td><a href='#'>Delete</a></td>"
    + "</tr>"
    $("#table").append(row);
}
function hideStudents() {
    $("#students").hide();
}

But I am getting this error:

Uncaught ReferenceError: check is not definedonclick @ StudentManagement.aspx:1

protected void Page_Load(object sender, EventArgs e)
    {
        BLLayer std = new BLLayer();
        list = std.GetAllStudents();
        var json = JsonConvert.SerializeObject(list);
        //hfListData.Value = json;
        StringBuilder strScript = new StringBuilder();
        strScript.Append("<script type=\"text/javascript\">");
        strScript.Append("var arr='");
        strScript.Append(json);
        strScript.Append("';");
        strScript.Append("</script>");

        ClientScriptManager script = Page.ClientScript;

        if (!script.IsClientScriptBlockRegistered(this.GetType(), "Var"))
        {
            script.RegisterClientScriptBlock(this.GetType(), "Var", strScript.ToString());
        }

I used this on page load to create array. And pass server side variable to client side by registering event.

FIDDLE

$(document).on('click', '.qwe', function() {
    alert($(this).closest('tr').find('td:nth-child(1)').text());


});

I tried making an onclick event for each anchor by adding a class for each anchor.Please check. DOCU

Use just this. It will work: jsfiddle.net/sherali/W4Km8/6080

window.check = function() { alert("Goood"); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM