简体   繁体   English

如何使用$(this)将ID的值获取到Ajax中?

[英]How can I get the value of an ID into Ajax using $(this)?

Here I have 4 buttons. 在这里,我有4个按钮。 How can I get the value of the id for each button onto the end of data: "id=" within the ajax part? 如何获取每个按钮的id值到data: "id="末尾data: "id=" ajax部分中的data: "id=" Bare in mind, each id is unique as its using a numeric counter. 简而言之,每个id都是唯一的,因为它使用数字计数器。

So for examples sake, if I clicked the third button then the id would equal 3 , if I clicked the second button, the id would 2... and so on. 例如,如果我单击第三个按钮,则id等于3 ;如果单击第二个按钮,则id等于2 ...依此类推。 Using $("a").attr('id') kind of works but it will grab the first <a> tag and stick with that value for which ever button you press. 使用$("a").attr('id')类的作品,但它将获取第一个<a>标记并坚持该值,无论您按下哪个按钮。 I also tried $(this).attr('id') but that doesn't work either. 我也尝试了$(this).attr('id')但这也不起作用。

Any suggestions? 有什么建议么?

 <?php 
    $counter = 0;

        echo '
      <button><a class="test" id="'.++$counter.'">Test</a></button>
      <button><a id="'.++$counter.'">Test</a></button>
      <button><a id="'.++$counter.'">Test</a></button>
      <button><a id="'.++$counter.'">Test</a></button>
    ';

    ?>
      <script id="source" language="javascript" type="text/javascript">

      $("button").click(function () 
      {

        //-------------------------------------------------------------------------------------------
        // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
        //-------------------------------------------------------------------------------------------
        $.ajax({                                      
          url: 'api.php',                  //the script to call to get data          
          data: "id="+ $("a").attr('id'),                        //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
          dataType: 'json',                //data format      
          success: function(data)          //on recieve of reply
          {
            var id = data[0];              //get id
            var vname = data[1];           //get name
            //--------------------------------------------------------------------------------------
            // 3) Update html content
            //--------------------------------------------------------------------------------------
            $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname);
          }
        }); 
      }); 
      </script>

As suggested in the comments, this should work. 如评论中所建议,这应该起作用。

$(this).find('a').attr('id')

If you didn't want to traverse the DOM, however, you could just store the value in the button. 但是,如果您不想遍历DOM,则可以将值存储在按钮中。

<button counterid="'.++$counter.'"><a class="test">Test</a></button>
<button counterid="'.++$counter.'"><a>Test</a></button>
<button counterid="'.++$counter.'"><a>Test</a></button>
<button counterid="'.++$counter.'"><a >Test</a></button>

Use the above for your HTML and then your JS would be: 将以上内容用于HTML,然后您的JS将是:

$(this).attr('counterid')

I don't know php but the idea is the same. 我不知道php,但是想法是一样的。

$("button").click(function () {
  //-------------------------------------------------------------------------------------------
  // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
  //-------------------------------------------------------------------------------------------
   var id = $(this).find('a').attr('id');
   $.ajax({                                      
     url: 'api.php',                  //the script to call to get data          
     data: "id="+id,                        //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
     dataType: 'json',                //data format      
     success: function(data) {
       var id = data[0];              //get id
       var vname = data[1];           //get name
       //--------------------------------------------------------------------------------------
      // 3) Update html content
      //--------------------------------------------------------------------------------------
      $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname);
    }
  }); 
}); 

$("a").attr('id') will retrieve all the anchor tags on the page and give you the first one's id. $("a").attr('id')将检索页面上的所有锚标记,并为您提供第一个ID。

In the context of your event handler function, this refers to the button element which was clicked. 在事件处理程序函数的上下文中, this表示单击的按钮元素。

To get the anchor tag inside the clicked button, you can use jQuery's .find method: 要在单击的按钮获取定位标记,可以使用jQuery的.find方法:

$(this).find('a').attr('id');

This will find any child anchor tags and return the id attribute of the first one. 这将找到所有子锚标签并返回第一个的id属性。

 $("button").click(function (e) {

   var id = $(e.target).attr('id');

    $.ajax{ 
        //...make the ajax call as needed
    });
});

e is the event, e.target is the button element that triggered the click, you can then get its ID directly. e是事件,e.target是触发单击的按钮元素,然后可以直接获取其ID。

example http://jsfiddle.net/ZRA9K/ 例如http://jsfiddle.net/ZRA9K/

See http://api.jquery.com/click/ 请参阅http://api.jquery.com/click/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM