Get Control's id from which jquery is called

Answered Get Control's id from which jquery is called

  • 2011年9月8日 12:05
     
     
    I have several radiobuttons.
    I have attached my jquery to this radio buttons
    When i call this jquery i want the control's id from which this jquery is called.

    i tried following technique
    $(this).attr('id');
    but i failed.

    How to find this.
    Thank you
    markand

全部回复

  • 2011年9月9日 1:44
     
     已答复 包含代码

    There no need to do that, as this is the plain DOM object itself.

    Using this.id will be enough. Or if you really want it that way, use $("#" + this.id).attr("id") .


    EDIT: Btw, I went to W3Schools to do some testing, and found that your's should work.

    <html>
    <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("p").click(function(){
        alert($(this).attr("id"));
        $(this).hide();
      });
    });
    </script>
    </head>
    <body>
    <p id="a">If you click on me, I will disappear.</p>
    </body>
    </html>
    

    There should be other problem that make it broken. Will you post the code here?

  • 2012年5月10日 7:13
     
     

    console.log($("#" + this.id).attr("id"));

    by using this line, we will get control id.