使用jquery从json获取动态表字段id

时间:2017-05-03 13:51:53

标签: jquery

我想点击删除按钮点击我resourceId函数中的当前roleIddeleteResource(),但我得到了undefined。我做错了什么?< / p>

function DeleteResource(elem) {
   var updatedBy = $("#userName").text(); 
   var url = document.URL;
  currentProjId = decodeURI(/id=([^&]+)/.exec(url)[1]);
   var resourceId = $('#' + elem).find(':nth-child(1)').attr('id');
   alert("resource id is" + resourceId +);
  var roleId = $('#' + elem).find(':nth-child(2)').attr('id');
    alert("role is is" + roleId +);
  }

https://jsfiddle.net/vuxL6qvx/

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var resourceList = [{
    "ResourceName": "john",
    "ResourceID": "1",
    "RoleId": "2",
    "RoleName": "admin"
},
{
    "ResourceName": "jute",
    "ResourceID": "2",
    "RoleId": "3",
    "RoleName": "employee"
}]

$(document).ready(function () {
    table()
});
function table() {
    for (i = 0 ; i < resourceList.length ; i++) {
        var resourceTable = '<tr id="' + (i + 1) + '"><td id="' + resourceList[i].ResourceID + '">' + resourceList[i].ResourceName + '</td><td class="RoleName" id="' + resourceList[i].RoleId + '">' + resourceList[i].RoleName + '</td><td><i class="fa fa-trash icon" id="dlt' + i + '" onclick="dltResource(this)"></i></td></tr>';

        $('#resourceTable').append(resourceTable)
    }
}
function dltResource(control) {
    //event.stopPropagation()
    id = $(control).closest('tr').attr('id');
    confirmDialog("Are you sure do you want to delete this Resource?", function () {
        DeleteResource(id)
    });
}
function DeleteResource(elem) {
    var updatedBy = $("#userName").text();
    var url = document.URL;
    //currentProjId = decodeURI(/id=([^&]+)/.exec(url)[1]);
    var resourceId = $('.projectResourceTable #' + elem + ' td:nth-child(1)').attr('id');
    alert("resource id is" + resourceId);
    var roleId = $('.projectResourceTable #' + elem + ' td:nth-child(2)').attr('id');
    alert("role is is" + roleId);
}

function confirmDialog(message, onConfirm) {
    var fClose = function () {
        modal.modal("hide");
    };
    var modal = $("#deleteProject,#deleteResource");
    modal.modal("show");
    $("#confirmMessage").empty().append(message);
    $("#confirmOk").one('click', onConfirm);
    $("#confirmOk").one('click', fClose);
    $("#confirmCancel").one("click", fClose);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

<div class="modal" id="deleteResource">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body" id="confirmMessage">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" id="confirmOk">Ok</button>
                    <button type="button" class="btn btn-default" id="confirmCancel">Cancel</button>
                </div>
            </div>
        </div>
    </div>

    <table class="table table-bordered table-responsive projectResourceTable">
        <thead class="colorBlue">
            <tr>
                <td>Resources</td>
                <td>Roles</td>
                <td>Action</td>
            </tr>
        </thead>
        <tbody id="resourceTable"></tbody>
    </table>
&#13;
&#13;
&#13;

您需要在其之前指定表.projectResourceTable的类名。 因为表中有多个具有相同ID的对象。

答案 1 :(得分:0)

updated your fiddle,更改了以下内容:

  1. 删除了onClick="..."
  2. 通过js:

    添加了onClick处理程序
            $('.icon').on('click', function(){
              dltResource($(this).parent().parent());
            })