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

时间:2019-03-11 13:07:09

标签: javascript jquery html append

我试图获取表列的每个值并将其追加到输入字段。到目前为止,我在下面有这段代码,它对[object Object]类中每个对象的每次单击都返回.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>

3 个答案:

答案 0 :(得分:3)

尝试一下,获取目标元素值并将其附加。

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

答案 1 :(得分:0)

要获取元素内部被点击的文本,请使用:

systemd-nspawn

其中$(this)代表被点击的元素,并在标记内向文本发送文本。

答案 2 :(得分:0)

检查下面的代码段。

let gesture = UILongPressGestureRecognizer(target: self, action: #selector(eyePressed(sender:)))
passwordEyeButton.addGestureRecognizer(gesture)

@objc func eyePressed(sender : UILongPressGestureRecognizer){
    switch sender.state {
    case .began :
        passwordEyeButton.setImage(eyeImage, for: .normal)
        passwordTextField.isSecureTextEntry = false
    case .ended :
        passwordEyeButton.setImage(crossedEyeImage, for: .normal)
        passwordTextField.isSecureTextEntry = true
    default :
        print("default state")
    }
}
/*$(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);
})