用jQuery指定div ID

I have buttons and divs and in each part I have them with the same ID I want to get the ID of button and use it for refreshing the div html.how should I write the * section?

   $(function() {
         $(".button").click(function(){ 
         var id=$(this).attr('id');
         var dataString = 'id='+ id ;
        $.ajax({
           type: "POST",
           url: "download_number.php",
           data: dataString,
           cache: false,
           success: function(html)
           {
          *********I HAVE PROBLEM HERE**************
            $('how to get the id of the div from var id of button above?').html(html);
          } 
           });

         });
              });

Div:

  Downloaded:<div id="<?php echo $id; ?>" ><?php echo $downloadcount;?></div>

Button:

 <input type = "button" value="Download" class="button" id="<?php echo $id; ?>" name="dl">

If I get class It will update the whole divs I want to update just the div realted to the button

Try something like:

$('.button').attr('id');

to get the id of the button, then to change it:

$('.button').attr('id',''); //delete previous id if existing
$('.button').attr('id','yourNewId'); //set new id

then to use the new id:

$("#yourNewId").doSomething();

change your html to this:

Downloaded:<div id="<?php echo $id; ?>" class="downloaded" ><?php echo $downloadcount;?></div>

then do something like:

var element_id = $(".downloaded").prop("id");
if(element_id = this.id){
   $("#"+element_id).html(/* ... */);
}
$(function() {
    $(".button").click(function(){ 
        var id=$(this).attr('id');
        var dataString = 'id='+ id ;
        $.ajax({
        type: "POST",
        url: "download_number.php",
        data: dataString,
        cache: false,
        success: function(html)
        {
            $('.count-' + id).html(html); // for class
        } 
        });

    });
});

<div class="count-<?php echo $id; ?>" ><?php echo $downloadcount;?></div>

Use the id but prefix them then build the name up...

<div id="div_<?php echo $id; ?>" ><?php echo $downloadcount;?></div>

button:

<input type = "button" value="Download" class="button" id="<?php echo $id; ?>" name="dl">

Then in you're code you have the id used in the buttons already (and also it will be div_), so you can then in you're 'success' just do:

$("#div_"+id).html(html);

First of all avoid using same IDS.
Then you can use CSS selectors:

$('div.class') //div
$('input[type="button"].youridclass')

You cannot have the same id on both the button and the div, id values must be unique in a document.

What I'd probably do is put the div's id on the button as a data-divid attribute (all attributes with the prefix data- are valid on all elements as of HTML5, and harmless in earlier versions of HTML), like this:

<input type="button" value="Download" class="button" data-divid="<?php echo $id; ?>" name="dl">

Then change

var id=$(this).attr('id');

to

var id=$(this).attr('data-divid');

...and then use that id var in your success callback (as the callback is a closure created within the context where id is defined, and so the callback has access to id).

Here's a simple example: Live copy | source

HTML:

<div id="div1">This is div1</div>
<div id="div2">This is div2</div>
<div>
  <input type="button" data-divid="div1" value="Update div1">
  <input type="button" data-divid="div2" value="Update div2">
</div>

JavaScript:

jQuery(function($) {
  $("input[type=button]").click(function() {
    var id = $(this).attr("data-divid");

    // I'll use setTimeout to emulate doing an ajax call
    setTimeout(function() {
      // This is your 'success' function
      $("#" + id).html("Updated at " + new Date());
    }, 10);

    return false;
  });
});

You cannot use the id attribute for that purpose, the id cannot be a number (valid html) and thereby id's needs to be unique. Use the data attrib instead.

First and foremost, ids should be unique, you'll run into problems, particularly when using jQuery, if you have elements with the same id.

Without seeing your markup it's hard to give you a working example. But you can get the id of the div which corresponds to the clicked button by traversing the DOM.

Example markup:

<div id="example-div">
  <input type="button" value="Example" />
</div>

jquery

$('input[type="button"]').click(function() {
  console.log($(this).parent('div').prop('id'));
}); 

// outputs 'example-div'

for your reference check the below link for the various ways that you can use to select the dom elements given the parent element.
jsperf.com/jquery-selectors-context/2