简体   繁体   English

jQuery:如何获取表格单元格数据并将其附加到输入字段?

[英]jQuery: how to get table cell data and append it to input field?

I am trying to get each value of table column and append to input field. 我试图获取表列的每个值并附加到输入字段。 So far I have this code below and it returns [object Object] on each click of each object from .token class. 到目前为止,我在下面有这个代码,并且每次点击来自.token类的每个对象都会返回[object Object] How do I reference the particular object that is being clicked to append to the input field? 如何引用要单击的特定对象以附加到输入字段?

 $(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> 

Try this, get the targetted element value and append it. 试试这个,获取目标元素值并附加它。

 $(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> 

For take the text inside element which get clicked, use: 要获取单击的元素内的文本,请使用:

var token = $(this).text()

Where $(this) represent the element clicked and text the text inside the tag. 其中$(this)表示单击的元素并在标记内部显示文本。

check snippet below.. 检查下面的代码段..

 /*$(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> 

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

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