简体   繁体   English

如何使用输入字段将文本插入表格

[英]How to insert text into table with input field

This is the html code 这是HTML代码

        <input type="text" id="input"/>
    <table>
        <tr>
            <td id="output"></td>
        </tr>
    </table>
<input type="button" id="submit" value="Submit">

And javascript 和JavaScript

$("#submit").click(function(){

  var input = document.getElementById("input").innerhtml;
  document.getElementById("output").innerHTML = input;

});

This javascript does't work whats the correct code? 这个JavaScript无法正常运作,正确的代码是什么?

Looks like you're using jQuery . 看起来您正在使用jQuery You can refactor your javascript to this: 您可以将JavaScript重构为:

$("#submit").click(function(e) {
  var inputValue = $('#input').val();
  $('#output').text(inputValue);
});

If you are using jquery use this. 如果您使用的是jQuery,请使用它。

$('#output').html('your input text');

if you are using native javascript use this 如果您使用的是本地javascript,请使用此

document.getElementById("output").html('your input text');

Hope this helps. 希望这可以帮助。

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

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