我试图获取表列的每个值,并将其附加到输入字段。到目前为止,下面有下面的代码,它在每次单击.token类中的每个对象时返回.token。如何引用被单击以附加到输入字段的特定对象?
$(function () {
$('.token').on('click', function () {
var url = $('#url');
var tkn = $('.token');
url.val(url.val() + tkn);
});
});<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>
发布于 2019-03-11 13:12:15
试试这个,获取目标元素值并追加它。
$(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>
发布于 2019-03-11 13:11:32
要获取被单击的文本内部元素,请使用:
var token = $(this).text()其中$(this)表示单击的元素,并在标记中对文本进行文本处理。
发布于 2019-03-11 13:16:14
检查下面的片段。
/*$(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);
})<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>
https://stackoverflow.com/questions/55102530
复制相似问题