单击时执行操作多个具有相同类的 href

Onclick perform action multple hrefs with same class

本文关键字:同类 href 执行 操作 单击      更新时间:2023-09-26

好的,我有一个for循环,可以从数据库中获取随机数据。我已经将这些值分配给我的 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">
和 href

单击我希望 href 值进入我的复选框,我使用以下代码来执行此操作:

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

只有第一个 href 正在更新输入,当我单击链接 2,3 等时,他们不会更新输入。

> beause 你没有引用被点击的那个

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

您需要获得您点击的那个

$(this).attr('href')