简体   繁体   English

输入文本字段到td

[英]Input text field to td

Im making a website that the user will put its Name and LastName and it will send the text in the table data . 即时消息使一个网站,用户将其名称和姓氏并将其发送到表数据中的文本。 My first inputs is showing in first table data 我的第一个输入显示在第一个表数据中

I need to put the second inputs in the second table data (can someone help me !) lol 我需要将第二个输入放入第二个表数据中(有人可以帮我!)

<h1 align="center">INFORMATION</h1>
Name: <input id="first_name" size="30" type="text">
LastName: <input type="text" size="30" id="lastname">
<button class="okok" name="myBtn" type="submit"  value="Submit Data" onClick="ajax_post();">GO </button>
<br />
<table border="2" cellspacing=1 align="center" height="100" width="600"> 
    <tr bgcolor="black">
        <th width="20" style="color:white;">Name</th>
        <th width="20" style="color:white;">Lastname</th>
    </tr>

    <tr>     
        <td style="color:white;" id="firstname"> </td>
        <td style="color:white;" id="firstlastname"> </td>
    </tr>

    <tr>
        <td style="color:white;" id="secondname"> </td>
        <td style="color:white;" id="secondlastname"> </td>
    </tr>
</table>

My script 我的剧本

function ajax_post(){
    var fn = document.getElementById("first_name").value;
    var table = document.getElementById("firstname");
    table.innerText = fn;

    var pre = document.getElementById("lastname").value;
    var table1 = document.getElementById("firstlastname");
    table1.innerText = pre;
}

I have no idea how to get the second input and put it in secondname and secondlastname without creating a new cell ! 我不知道如何获取第二个输入并将其放在secondname和secondlastname中,而无需创建新单元格!

OK then, from what you have and what you need I think this can do what you want to do. 好吧,从您的拥有和需求出发,我认为这可以完成您想做的事情。

But if you could use jQuery then we may had easier ways... (I have changed some 'ids' to simplify the code) 但是,如果您可以使用jQuery那么我们可能会有更简单的方法...(我更改了一些“ id”以简化代码)

HTML 的HTML

<h1 align="center">INFORMATION</h1>
Name: <input id="txtFirstname" type="text" size="30">
<br />
LastName: <input id="txtLastname" type="text" size="30">
<br />
<button class="okok" name="myBtn" type="submit" value="Submit Data" onClick="ajax_post();">GO </button>
<br />
<table border="2" cellspacing="1" align="center" height="100" width="600"> 
    <tr bgcolor="black">
        <th width="20" style="color:white;">Name</th>
        <th width="20" style="color:white;">Lastname</th>
    </tr>

    <tr>     
        <td style="color:red;" id="tdFirstname1"> </td>
        <td style="color:red;" id="tdLastname1"> </td>
    </tr>

    <tr>     
        <td style="color:red;" id="tdFirstname2"> </td>
        <td style="color:red;" id="tdLastname2"> </td>
    </tr>
</table>

JavaScript 的JavaScript

First I get a JavaScript variable to indicate which row I'm writing the data and set it to 1: 首先,我得到一个JavaScript变量来指示我正在写数据的那一行,并将其设置为1:

var rowNum = 1;

and then comes your function with modification: 然后对您的函数进行修改:

function ajax_post(){
  if(rowNum < 3){
    var txtFirstname = document.getElementById("txtFirstname").value;
    var txtLastname = document.getElementById("txtLastname").value;

    document.getElementById("tdFirstname"+rowNum).innerText = txtFirstname;
    document.getElementById("tdLastname"+rowNum).innerText = txtLastname;

    rowNum++;
  }
}

also you can find a working fiddle here 你也可以在这里找到一个工作的小提琴

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

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