简体   繁体   English

设置输入字段的值,基于点击一组 href 链接

[英]Set value of input field, based on click on a set of a href links

I have this HTML Code, with few links, as :我有这个 HTML 代码,链接很少,如:

 $(function() { $('#brad_button_770103363').click(function() { //alert($(this).text()); var paypal = $(this).text(); //$('#paypal_donate_amount').val(paypal).trigger('change'); $('#paypal_donate_amount').val(paypal); var paypal = null; console.log(paypal); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="" id="brad_button_770103363">$20</a> <a href="" id="brad_button_770103363">$50</a> <!-- similar 10 links... --> <input type="text" id="paypal_donate_amount" />

Now, I want that for each new click, on ahref, its val() ie 20, 50 Nmerical value only, is updated in this input field & overwrites old value of input.现在,我希望对于 ahref 上的每次新点击,其 val() 即 20, 50 Nmerical 值仅在此输入字段中更新并覆盖输入的旧值。

But, it simply updates value of 1st click only.但是,它只更新第一次点击的值。 On subsequent, click nothing happens in input field values.随后,单击输入字段值中没有任何反应。 Where i am wrong ?我错在哪里? Also, how to remove $ sign ?另外,如何删除 $ 符号? Plz help.请帮忙。

This works:这有效:

 $(function(){ $('.brad_button_770103363').click(function(){ var paypal = $(this).text(); $('#paypal_donate_amount').val(paypal); var paypal = null; }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="javascript:void(0)" class="brad_button_770103363">$20</a> <a href="javascript:void(0)" class="brad_button_770103363">$50</a> <input type="text" id="paypal_donate_amount" />

What I have done is adding href="javascript:void(0)" so it doesn't behave like a link.我所做的是添加href="javascript:void(0)"因此它的行为不像链接。 And the IDs became classes, so if they are repeated, it's not a problem.并且 ID 变成了类,因此如果它们重复,则不是问题。

If you have control over the HTML content, I would suggest something like this...如果您可以控制 HTML 内容,我会建议这样的...

 $(function() { $('.donateAmount').click(function() { $('#paypal_donate_amount').val($(this).data('amount')); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="#" class="donateAmount" data-amount=20>$20</a> <a href="#" class="donateAmount" data-amount=50>$50</a> <input type="text" id="paypal_donate_amount" value="0" />

change the id to class as id is unique, unlike class将 id 更改为 class,因为 id 是唯一的,与 class 不同

<a href="" class="brad_button_770103363">$20</a>
<a href="" class="brad_button_770103363">$50</a>

<input type="text" id="paypal_donate_amount" />

<script type="text/javascript">

$.noConflict();


$(function(){
    $('.brad_button_770103363').click(function(){
        //alert($(this).text());
        var paypal = $(this).text();
        //$('#paypal_donate_amount').val(paypal).trigger('change');
        $('#paypal_donate_amount').val(paypal);
        var paypal = null;
        console.log(paypal);

    });
});

try the code and get back, did not test it for errors尝试代码并返回,没有测试它是否有错误

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

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