gpt4 book ai didi

javascript - 获取通过click函数中的href链接传递的id

转载 作者:行者123 更新时间:2023-11-28 16:07:54 26 4
gpt4 key购买 nike

我有一个相当直接的问题。为什么我不能在点击函数中使用 $('a.view').attr('id') (代码中引用//1)?我尝试了它,但它无法工作,但 this.id 可以工作。我想我主要想知道下面代码上下文中的差异:

displayRecord.php(以下链接调用click函数):

echo '<td><a href="#" style="text-decoration: none;" id="'.$data['id'].'" class="view" ><input type="button" value="View" /></a></td>';

editTicket.php:

$('a.view').click(
function(e)
{
//1
var ticket_id = this.id;

dlg.load('displayRecord.php?id='+this.id, function(){

var escalationValue = '';

$.post('escalateValue.php',{post_ticket_id:ticket_id},
function(data) {

if (data == 'No'){
showCount();
}
});

dlg.dialog('open');

});

});

最佳答案

$('a.view').attr('id') 可以匹配多个元素,如果您有多个带有 view 类的 anchor ,那么您将如果您在 click 事件中使用它,则不一定获得单击的元素。 this.id 仅指被单击的元素,这也是最快的方法,但为了演示您也可以这样做:

$(this).attr('id'); // in the click event

关于javascript - 获取通过click函数中的href链接传递的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14119788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com