使用jQuery插入表行
Insert a Table Row using jQuery
这是我的文凭项目,我想在开头插入一行选择设备,但我不知道如何使用jQuery。
<form>
<table border="1">
<tr>
<td>Kod Peralatan</td>
<td>Nama Peralatan</td>
</tr>
<tr>
<td>
<select>
<option>Choose Equipment</option>
</select>
</td>
<td> </td>
</tr>
</table>
<table>
<tr>
<td align="right">
<button type="button">Add Row</button>
</td>
</tr>
</table>
</center>
</form>
我想这就是您可能想要的。这将动态地向设备表中添加一个新行,并创建一个新的下拉列表,该列表将被填充。
<!DOCTYPE html>
<html>
<head>
<title>Equipment</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
var rows = 0;
function addMoreEquipment() {
rows++;
var options = [{ name: "Item 1", value: "1" }, { name: "Item 2", value: "2" }, { name: "Item 3", value: "3" }];
var row = $("<tr id='row"+ rows +"'><td><select id='equipment" + rows + "'></select></td><td><a href='#' onclick='removeRow('"row" + rows + "'")'>Remove</a></td></tr>")
$("#equipment").append(row);
for (var i = 0; i < options.length; i++)
$("#equipment" + rows).append(new Option(options[i].name, options[i].value));
}
function removeRow(id) {
$("#" + id).remove();
}
$(document).ready(function() {
addMoreEquipment();
});
</script>
</head>
<body>
<form>
<table border="1" id="equipment">
<tr>
<td>Kod Peralatan</td>
<td>Nama Peralatan</td>
</tr>
</table>
<div style="text-align: right">
<input type="button" onclick="addMoreEquipment()" value="Add Row">
</div>
</form>
</body>
</html>
你可以在这里查看更新后的JS Fiddle:http://jsfiddle.net/b3gYk/1/
$("button").click(function() {
$("select").append(new Option('Foo', 'foo', true, true));
});
这是一个演示
相关文章:
- 如何动态插入jquery代码
- 换行符插入jquery选择器
- 将方法插入jQuery插件
- 在另一个脚本后插入Jquery
- 我应该如何在提交操作之前插入jQuery代码
- 创建 var 并将其插入 jQuery Child Selector(“父>子”)
- 如何在页脚中插入 jQuery 时定义变量
- 阻止第三方JavaScript插入jQuery
- 如何将HTML文本插入jQuery
- 如何将PHP插入jQuery
- 通过PHP在MySQL中插入Jquery
- 如何在PHP代码中插入jQuery插件?
- 在DOM中插入jQuery验证代码
- 使用userscript在header中插入jQuery和jQuery UI
- 在对象id:中插入jquery字符串
- 在表模板中插入jQuery输入
- Jquery插入Jquery属性
- 在带有事件的另一个浏览器选项卡中插入jQuery对象
- 如何在php数组中插入jquery值
- 难以将变量插入jquery属性标识