简体   繁体   English

使用 jQuery 将内容复制到文本框中

[英]Copy content from <td> into a text box using jQuery

With the code below, I don't seem to be able to just set the textbox to the value of an TD element.使用下面的代码,我似乎无法将文本框设置为 TD 元素的值。 What I need to achieve is to populate Enter Refund Amount box with the already present value of Grand Total field above.我需要实现的是用上面总计字段的现有值填充输入退款金额框。 Nothing else is needed except copying the content from one element to the other.除了将内容从一个元素复制到另一个元素之外,不需要其他任何东西。

window.onload = function() {
    var src = document.getElementById("grand_total").innerHTML;
    dst = document.getElementById("refund_amount").innerHTML;
    src.addEventListener('#refund_amount', function() {
        dst = src;
    });
};

在此处输入图像描述

Firstly, #refund_amount isn't a valid event name.首先, #refund_amount不是有效的事件名称。 Given the context of the code and your goal I would assume that should be click instead.鉴于代码的上下文和您的目标,我认为应该改为click You also need to bind the event handler on the Element reference, not the a string variable containing its innerHTML value.您还需要在 Element 引用上绑定事件处理程序,而不是包含其innerHTML值的字符串变量。 In addition, to set the value of an input element you need to use the value property of the Element directly.此外,要设置input元素的值,您需要直接使用元素的value属性。 Try this:尝试这个:

 var src = document.getElementById("grand_total"); var dst = document.getElementById("refund_amount"); src.addEventListener('click', function() { dst.value = src.textContent; });
 <table> <tbody> <tr> <td>Grand total:</td> <td id="grand_total">26.58</td> </tr> <tr> <td>Refund amount:</td> <td><input type="text" id="refund_amount" /></td> </tr> </tbody> </table>

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

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