正在检索添加的jquery行
Retrieving added jquery rows
检索用户添加的行中的所有数据时遇到问题。我尝试使用我的变量reclist
来检索记录,但它给了我空白。
源代码如下:
<script type="text/javascript">
$(document).ready(function(){
$('#btnAdd').live('click',function(){
var name = $('#txtName').val();
var name2 = $('#txtName2').val();
$("#tbNames tr:last").after("<tr><td>" + name + "</td><td>" + name2 + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
});
$('#tbNames td img.delete').live('click',function(){
$(this).parent().parent().remove();
});
});
</script>
<input id="txtName" type="text" />
<input id="txtName2" type="text" />
<input id="btnAdd" type="button" value="Add" />
<table id="tbNames" >
<tr>
<td><b>Name</b></td>
<td><b>Name2</b></td>
<td><b>Delete</b></td>
</tr>
<tr>
<td>Bingo</td>
<td>Tingo</td>
<td><img src="Delete.gif" height="15" class="delete" /></td>
</tr>
</table>
<script type="text/javascript">
$(document).ready(function() {
$("#submit_app").button();
$("#submit_app").click(function() {
var reclist = $("#txtName").val();
console.log(reclist);
});
});
</script>
<input id="submit_app" type="button" style="height: 35px; width: 225px" value="Add New User" />
如果我错过了任何编码,请告知。
尝试以下代码。请在jsFiddle.net上查看此演示。这将为您提供表的td
中的所有值。
$(document).ready(function() {
$('#btnAdd').live('click', function() {
var name = $('#txtName').val();
var name2 = $('#txtName2').val();
$("#tbNames tr:last").after("<tr><td>" + name + "</td><td>" + name2 + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
});
$('#tbNames td img.delete').live('click', function() {
$(this).parent().parent().remove();
});
//$("#submit_app").button(); //Commented this line
$("#submit_app").click(function() {
var skipFirstRow = 0; //Skip first row
$("#tbNames tr").each(function() {
if (skipFirstRow++ == 0) return;
var skipLastColumn = 0;
$(this).children("td").each(function() {
if (skipLastColumn++ == 2) return;
alert($(this).html());
});
});
var reclist = $("#txtName").val();
console.log(reclist);
});
});
编辑
检查一下这把小提琴。在这里,您将获得两个数组中的值。
$("#txtName")是指您在中声明的文本框
<input id="txtName" type="text" />
由于您想检索已插入表中的所有数据,因此应该使用$("td","#tbNames")并检索它们的.text()
$("td", "#tbNames").each(function() {
console.log($(this).text());
});
您需要将click事件绑定为live。由于正常绑定只会将事件绑定到首次呈现的页面上找到的元素
这是更正的部分:
<script type="text/javascript">
$(document).ready(function() {
$("#submit_app").button();
$("#submit_app").live('click', function() {
var reclist = $("#txtName").val();
console.log(reclist);
});
});
</script>
您的提交按钮未被触发
http://jsfiddle.net/gBHh2/
相关文章:
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 通过jQuery添加ng样式属性,angular不更新
- 使用HTML5 localStorage和jQuery添加到收藏夹/书签
- jQuery添加的文本不显示
- 使用Jquery添加内容
- 如何使用jQuery添加另一个对象的高度作为边距
- JavaScript/jQuery-添加添加和删除类与下一个函数之间的延迟
- 对于每个json结果,使用类jquery添加css
- Jquery添加一个类之后,如何应用css
- 使用jQuery添加和删除属性
- Jquery添加输入字段和日期选择器
- jQuery - 添加删除类 - 设置输入值
- 为什么在这种情况下要向JQuery添加两个链接
- 为图像滑块jquery添加项目符号功能
- 使用jquery添加到表单内部表中的元素不会过帐
- 如何使用jquery添加元素
- 使用Jquery添加XML元素
- 当使用javascript/jquery添加值时,html文本输入表显示不正确
- 将jquery添加到firefox扩展中
- 运行 AJAX 需要什么 - 将 jQuery 添加到页面处理 AJAX 调用