试试这个,获取目标元素值并附加它。
$(function () { $('.token').on('click', function (event) { var url = $('#url'); var tkn = event.target url.val(url.val() + tkn.textContent); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="form-group"> <div class="col-sm-10"> <input type="url" class="form-control" name="e_postback_url" size="60" placeholder="URL" value="URL" id="url" maxlength="1024"> </div> </div> <table class="table table-bordered table-hover tc-table"> <thead> <tr> <th class="token">Token</th> <th>Type</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td class="token" id="token1">[commission_id]</td> <td class="typedesc">Numeric-16</td> <td class="typedesc">Unique order ID of the commission</td> </tr> <tr> <td class="token" id="token2">[offer_id]</td> <td class="typedesc">Numeric-10</td> <td class="typedesc">Unique offer ID</td> </tr> <tr> <td class="token" id="token3">[payout]</td> <td class="typedesc">Decimal-20,2</td> <td class="typedesc">Amount of the commission in EUR</td> </tr> </tbody> </table>
检查下面的代码段..
/*$(function () { $('.token').on('click', function () { var url = $('#url'); var tkn = $('.token'); url.val(url.val() + tkn); }); });*/ var urlVal = $('#url').val(); $('.token').click(function(){ urlVal = urlVal + $(this).text(); $('#url').val(urlVal); console.log(urlVal); })
要获取单击的元素内的文本,请使用:
var token = $(this).text()
其中$(this)表示单击的元素并在标记内部显示文本。