JQUERY-动态生成的按钮的未定义数据属性值

时间:2018-11-24 07:51:59

标签: javascript jquery

具有预览按钮。单击它会启动一个函数,该函数通过ajax生成具有数据属性的新按钮,并附加到其他html。要获取生成的EDIT按钮的“ data-id”属性值,但该值显示为“ undefined”。

var answer_id;
$(document).on('click', '#preview', function(){//PREVIEW button clicked
    ...some code...
    $.ajax({...});
    //ajax retrieves data and appends to html code
    //this data contains buttons.  One of the buttons is:
    //<button id="edit" type="button" class="btn" value="edit" data-id="521">Edit</button>
    answer_id = $('#edit').attr("data-id");
    alert(answer_id);
});

我可以检查浏览器页面代码,并且编辑按钮具有预期的data-id值。 但是,出现的jquery警报显示为“未定义”。

我尝试过:

$('#edit').attr("data-id")
$("#edit").data("id")
$("#edit").data().value;

怎么了?

0 个答案:

没有答案
相关问题