如何在动态创建的元素

时间:2018-01-17 13:34:16

标签: javascript jquery asp.net-ajax

基于ajax调用我在页面上创建动态元素(按钮),我需要调用另一个ajax函数来设置其属性。

我想在其创建时设置其数据属性。

<button class="light UserInfo_" data-toggle="dropdown" aria-expanded="true"> data.DriverName </button>

我尝试了以下功能,但它无法正常工作。

 jQuery(document).on('DOMNodeInserted', '.UserInfo_', function () {
        jQuery(this).encryptIt();
    });
    function encryptIt() {      
        alert('hi');
    };

1 个答案:

答案 0 :(得分:0)

请尝试以下操作。

responseJSON:{element: "<input id='n1'>", id: "n1", attr: "value", value="Hi"}

function foo() {
    var result;

    $.ajax({
        url: '...',
        success: function(response) {
            result = response;                
        }
    });

    return result;
}

var result = foo().done(function(){

  console.log(result.responseJSON)

  $("#mainCont").append(result.responseJSON["element"]);

    $("#"+result.responseJSON["id"]).attr(result.responseJSON["attr"],result.responseJSON["value"])


}); 

<强> HTML

<div id="mainCont"></div>