我想点击删除按钮点击我resourceId
函数中的当前roleId
和deleteResource()
,但我得到了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 +);
}
答案 0 :(得分:1)
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;
您需要在其之前指定表.projectResourceTable
的类名。
因为表中有多个具有相同ID的对象。
答案 1 :(得分:0)
我updated your fiddle,更改了以下内容:
onClick="..."
通过js:
添加了onClick
处理程序
$('.icon').on('click', function(){
dltResource($(this).parent().parent());
})