从<a> tag

时间:2018-05-17 14:58:09

标签: javascript jquery ajax

I have the ff. code which passes the object response from AJAX via an <a> tag's id

"<td>" + "<a id='"+userData+"' class='edit' href=''>Edit</a>" + "</td>"

However, when I checked the value contained in userData when it was retrieve via event.target.id

$('.edit').click(function(ev){
    ev.preventDefault();
    console.log(ev.target.id); // this prints the output shown below
    show_add_user_modal();
});

I get the ff. output when I used console.log(ev.target.id);

[object Object],[object Object],[object Object],[object Object],[object Object]

Below is the entire ajax code.

$.ajax({
    url: 'controller/get_all_users.php',
    type: 'POST',
    dataType: 'json',
    success: function(userData){
        console.log(userData);
        var len = userData.length;
        $('#table_users_record').find("tr:not(:first)").remove();
        for (var i = 0; i < len; i++) {
            var userId = userData[i]['id'];
            var userName = userData[i]['username'];
            var lastName = userData[i]['lastname'];
            var firstName = userData[i]['firstname'];
            var middleInitial = userData[i]['middleinitial'];
            var roleName = userData[i]['roleName'];
            var isActive = userData[i]['isactive'];
            $('#table_users_record').append(
                "<tr><td>" + userId + "</td>" +
                "<td>" + roleName + "</td>" +
                "<td>" + userName + "</td>" +
                "<td>" + lastName + "</td>" +
                "<td>" + firstName + "</td>" +
                "<td>" + middleInitial + "</td>" +
                "<td>" + isActive + "</td>" +
                "<td>" + "<a id='"+userData+"' class='edit' href=''>Edit</a>" + "</td>" +
                "<td>" + "<a id='' href='#'>" + "Deactivate" + "</a>" + "</td>" +
                "</tr>"
            );
        }
        $('.edit').click(function(ev){
            ev.preventDefault();
            //do something with click
            //alert(ev.target.id);
            console.log(ev.target.id);
            show_add_user_modal();
        });
    }
});

What is the correct way to parse it when retrieved through event?

I'd appreciate any suggestion.

Thank you.

0 个答案:

没有答案