简体   繁体   English

Onclick与同一个类别执行动作多个href

[英]Onclick perform action multple hrefs with same class

Okay, I have a for loops which grabs random data from the database. 好的,我有一个for循环,可以从数据库中获取随机数据。 I have assigned those values to my a href like so: 我已经将这些值分配给我的href,如下所示:

<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: 并在href单击我希望href值进入我的复选框时,我正在使用以下代码执行该操作:

 $(".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. 只有第一个href会更新输入,当我单击链接2,3等时,它们不会更新输入。

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')

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

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