使用jQuery获取th的ID [英] Get the id of th with jQuery
问题描述
我有这张桌子:
<th id="a" width="250">Backlog</th>
<th id="b" width="250">Wip</th>
<th id="c" width="250">Testing</th>
<th id="d" width="250">DOD</th>
</tr>
我的要求
我有4栏待办事项,在制品,测试和mod.如果我从待办事项列表"拖到测试",我想获取下拉列ID. IE.我想将其保存到数据库.我试过了:
I have 4 columns backlog, WIP, Testing, DOD. If I drag from Backlog to Testing I want to get drop columns id. Ie. i want to save it to database. I tried this:
jQuery(function() {
jQuery(".draggable").draggable({
containment: "#containment-wrapper",
scroll: false,
stop: function(event, ui) {
// Show dropped position.
var id = $(this).attr('id');
var trid = $(this).closest('tr').attr('id');
alert(trid);
var Stoppos = $(this).position();
model = {
id: id,
left: Stoppos.left,
top: Stoppos.top
};
$.ajax({
url: "/scrum/save",
type: "post",
data: model,
success: function(data) {
jQuery.HP({
title: "Success!",
message: "Saved..."
});
},
error: function() {
// alert('error is saving');
}
});
}
});
});
我尝试使用此方法获取当前列的ID,但没有用.
I tried this to get id of current column, but it didn't work.
var trid = $(this).closest('th').attr('id'); alert(trid);
如何获取ID?
推荐答案
如果单击<th>
,则应通过以下行获取ID:
If you clicked the <th>
then you should get the id via the following line:
var id=$(event.target).attr('id');
(如果您在包含该事件的元素内单击某个元素,请使用"currentTarget",而不是"target").
(In case you click on an element withint an element that contains the event, use "currentTarget", instead of "target").
如果单击其他选项,则需要通过Parent()/Child()或其他方法导航到该位置.你有HTML吗?以及使用$(event.target)时返回HTML的哪个元素?
If you click on something else, then you need to navigate there via Parent()/Child() or other measures. Do you have the HTML? And which element if HTML is returned when using $(event.target) ?
这篇关于使用jQuery获取th的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!