您能告诉我如何在单击弹出选项时获取行的 ID 吗?当按下“添加”按钮时,我动态生成了该行。行上有一个图标“:”,单击图标时会显示弹出窗口屏幕上 当我单击“编辑”或其他选项时,我想显示它打开的行的 ID。我能够获取编辑事件。但无法获取 ID。 http://jsfiddle.net/4ajeB/5/
function createTestCase(testCaseName, iscreatedFromScript, jsonObject) {
var id;
if (typeof ($("#testCaseContainer li:last").attr('id')) == 'undefined') {
id = "tc_1";
var index = id.indexOf("_");
var count = id.substring(index + 1, id.length);
count = parseInt(count);
var conunter = count;
} else {
id = $("#testCaseContainer li:last").attr('id');
var index = id.indexOf("_");
var count = id.substring(index + 1, id.length);
count = parseInt(count);
var conunter = count;
id = id.substring(0, index) + "_" + parseInt(count + 1);
}
var html = '<div class="testcaselist_row">' + '<ul>' + '<li id="' + id + '" class="clickTestCaseRow"><a href="#" style="color: #ffffff!important;">' + id + '<i class="icon1 test_h"></i></a></li>' + '</ul></div>';
$('#testCaseContainer').append(html).enhanceWithin();
}
$('.edit_h').click(function(){
alert("edit"+$(this).id)
})
我使用全局变量获取了 ID。是否可以在不使用变量的情况下获取 ID?
最佳答案
只需将 ID 作为数据添加到 .edit_h
中,并在 click
事件中访问它即可。
...
$('.edit_h').data('originalId', id);
$('#testCaseContainer').append(html).enhanceWithin();
}
$('.edit_h').click(function(){
alert("edit ID:"+$(this).data('originalId'));
})
更新的 fiddle :http://jsfiddle.net/4ajeB/6/
关于javascript - 单击弹出选项时如何获取行的 Id(使用全局变量获取 Id)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24080588/