如何获取点击的div jQuery的ID [英] How to get id of clicked div jquery

查看:350
本文介绍了如何获取点击的div jQuery的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获取相同类但具有不同ID 的单击div的ID.我尝试了我所知道的一切.

这是我尝试的一个代码(在此代码中,.click function()不起作用 ..这意味着单击该代码似乎根本不运行该代码!):-

I have been trying to get the id of a clicked div of same classes but with different IDs. I tried everything I know.

Here is one code I tried (in this one, the .click function() does not work .. edit: meaning it does not seem to run the code when clicked at all!):-

$(".u_search").click(function() {
    var attr_u_search = $(this).attr('id');
    var dataString = '&u_search=' + $(".u_search").attr('id');
    alert(dataString);

    $.ajax({
    type: "POST",
    url: '/script/profile.php',
    data: dataString,
    cache: false,
    success: function(data) {
          $('#ui_profile').show();
          $('#ui_profile').html(data);
          location.hash = 'profile' + 'id=' + dataString;
          $(".searchbox").val('');
          $("#usr_suggest").hide();

    }
  });
});

PHP:-

echo "<tr id='" . $id . "' class='u_search' height='40px'><td width='40px'><img class='avatar' src='$avater' /></td><td>" . $fname_ . " " . $lname_ . "</td></tr>";
}}


这是我尝试过的另一种代码组合(此错误 :假设我有5个divs,即使我单击了第2个div或第3个div,它也只能捕获第一个div的ID. div [div 1]而不是单击的div.我希望能够捕获单击的div 的ID.):-


Here is another combination of codes I tried (error in this one: suppose I have 5 divs, and even if I clicked the 2nd div or the 3rd div, it only captures the id of the first div [div 1] and not the clicked div. I want to be able to capture the id of the clicked div.):-

$(".u_search").click(function() {
    var attr_u_search = $(".u_search").attr('id');
    var dataString = '&u_search=' + $(".u_search").attr('id');
    alert(dataString);

    $.ajax({
    type: "POST",
    url: '/script/profile.php',
    data: dataString,
    cache: false,
    success: function(data) {
          $('#ui_profile').show();
          $('#ui_profile').html(data);
          location.hash = 'profile' + 'id=' + dataString;
          $(".searchbox").val('');
          $("#usr_suggest").hide();

    }
  });
});

PHP:-

echo "<tr id='" . $id . "' class='u_search' height='40px' onclick='javascript:op_prof(1)'><td width='40px'><img class='avatar' src='$avater' /></td><td>" . $fname_ . " " . $lname_ . "</td></tr>";
}}

当我使用第一个代码(带有.click function()的代码)时,该代码似乎根本没有运行! ..我正在使用jquery库版本1.9.1

edit: when I use the first code (the one with .click function()) the code does not seem to run at all! .. I am using jquery library version 1.9.1

推荐答案

内部功能更改

$('.u_search').attr('id')

收件人

$(this).attr('id')

引号很重要,但这不是引号;)

The quotes are important, but this is not inside quotes ;)

这篇关于如何获取点击的div jQuery的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆