简体   繁体   中英

Onclick perform action multple hrefs with same class

Okay, I have a for loops which grabs random data from the database. I have assigned those values to my a href like so:

<input type="text" class="form-control text-center no-left-border" maxlength="11" id="sellamount" name="sellamount" value="0" />
  <a href="<?php echo $buyOrders->amount; ?>" class="buyorderamount">

and the href click I am wanting the href value to go in to my checkbox, I am using the following code to perform that action:

 $(".buyorderamount").click(function(e)
  {
    e.preventDefault();
    $('#sellamount').val($('.buyorderamount').attr('href'));
    return false;
  });

Only the first href is updating the input, when I click link 2,3 etc they don't update the input.

Beause you do not reference the one that was clicked

$('.buyorderamount').attr('href') <-- grabs all the elements and will return the first one

You need to get the one you clicked on

$(this).attr('href')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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