在表中创建带有输入的新行,然后单击保存该输入的值
Create new row with inputs in table and onclick save the value of this inputs
HTML:
<tbody>
<tr id="hiddenRow">
<td>
<button id="saveBtn" class="btn btn-success btn-xs">save</button>
</td>
<td id="firstName">
<input id="first" type="text" placeholder="first name" name="first_name" />
</td>
<td id="lastName">
<input id="last" type="text" placeholder="last name" name="last_name" />
</td>
<td id="date">
<input id="dateOf" type="text" placeholder="date of birth" name="date_of_birth" />
</td>
<td id="address">
<input id="addres" type="text" placeholder="address" name="address" />
</td>
<td id="phone">
<input id="phonenumber" type="text" placeholder="phone" name="phone" />
</td>
<td id="email">
<input id="mail" type="text" placeholder="email" name="email" />
</td>
<td id="left">
<input id="leftEye" type="text" placeholder="left eye" name="left_eye" />
</td>
<td id="right">
<input id="rightEye" type="text" placeholder="right eye" name="right_eye" />
</td>
</tr>
</tbody>
JS:
$('.new_customer').click(function () {
$("#hiddenRow").show();
$("#saveBtn").click((function () {
var firstval = $("#first").val(),
lastval = $("#last").val(),
dateval = $("#dateOf").val(),
addressval = $("#addres").val(),
phoneval = $("#phonenumber").val(),
emailval = $("#mail").val(),
leftval = $("#leftEye").val(),
rightval = $("#rightEye").val();
$("#firstName").text(firstval);
$("#lastName").text(lastval);
$("#date").text(dateval);
$("#address").text(addressval);
$("#phone").text(phoneval);
$("#email").text(emailval);
$("#left").text(leftval);
$("#right").text(rightval);
}));
这是Fiddle
我想在每次单击"new_customer"时创建新行,并在每次单击保存时保存新数据。我只是一个初学者,我知道我的代码很糟糕)我会很高兴有任何想法。我如何用上面的方法做到这一点?感谢
var new_row='<tr>
<td>
<button id="saveBtn" class="btn btn-success btn-xs">save</button>
</td>
<td id="firstName">
<input id="first" type="text" placeholder="first name" name="first_name" />
</td>
<td id="lastName">
<input id="last" type="text" placeholder="last name" name="last_name" />
</td>
<td id="date">
<input id="dateOf" type="text" placeholder="date of birth" name="date_of_birth" />
</td>
<td id="address">
<input id="addres" type="text" placeholder="address" name="address" />
</td>
<td id="phone">
<input id="phonenumber" type="text" placeholder="phone" name="phone" />
</td>
<td id="email">
<input id="mail" type="text" placeholder="email" name="email" />
</td>
<td id="left">
<input id="leftEye" type="text" placeholder="left eye" name="left_eye" />
</td>
<td id="right">
<input id="rightEye" type="text" placeholder="right eye" name="right_eye" />
</td>
</tr>';
$("#hiddenRow tbody tr:last").after(new_row);
在添加新行按钮上添加此代码,并为保存数据编写代码
我想这就是你想要的一个例子:
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>
<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}
</script>
</body>
</html>
参考和工作示例
相关文章:
- 单击jquery清除输入值
- 使用jquery在单击时在单元格中输入值
- 主体单击删除功能上的输入框宽度
- Javascript:;单击“;以及“;输入键“;
- 单击鼠标,用MySQL数据填充html表单输入字段
- 使用 jQuery,当我在输入框外部单击时,如何更改输入元素的值
- 使用jQuery切换来显示内容,但防止在用户单击输入元素时关闭
- 单击其他元素或鼠标向上时隐藏输入框,但保留一次焦点
- 防止在输入字段上单击事件
- 如何在单击时从输入字段中删除最后一个字符
- 通过单击链接(兄弟姐妹?)从无线电输入中获取id
- 单击下拉菜单时,将文本粘贴到输入框中
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 单击表单1中的按钮:更改表单2中隐藏输入的值(纯javascript)
- 调用时未输入角度 ng 单击函数
- 在 javascript 事件单击后清空输入
- 如何在输入单击时出现键盘时使标题粘在顶部 (iPhone)
- 加载页面后未触发文件输入单击事件
- 如何在输入更改事件之前获得Bootstrap预先输入单击结果
- 无法识别输入单击