JQuery - 通过 $.each 循环在表中查找文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9819038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery - Find textbox values within a table through $.each loop
提问by Manikandan Sethuraju
I have one HTML Table... This HTML table's first row is a static, when they click one (+) button means, the rows will added dynamically, the user want to delete one row means, he click one(-) button means current row is delete.
我有一个 HTML 表格...这个 HTML 表格的第一行是静态的,当他们点击一个 (+) 按钮时,行将动态添加,用户想要删除一行意味着,他点击一个 (-) 按钮意味着当前行被删除。
Each row have 4 text-boxes. My Jquery code is give below..
每行有 4 个文本框。我的 Jquery 代码如下..
var FirstName;
var LastName;
var Email;
var PhoneNumber;
$("#tableId tr").find('fieldset').each(function (i) {
FirstName =FirstName +','+ $("#txtFirstName" + (i + 1) + "").val();
LastName =LastName +','+ $("#txtLastName" + (i + 1) + "").val();
Email =Email +','+ $("#txtEmail" + (i + 1) + "").val();
PhoneNumber = PhoneNumber+','+ $("#txtPhoneNumber" + (i + 1) + "").val();
});
I set the text-boxes id's are dynamically,its working fine, but when the user delete one row means i cant get the text-boxes value based on Id.
我设置的文本框 id 是动态的,它工作正常,但是当用户删除一行时意味着我无法根据 Id 获取文本框值。
How do i get the text-boxes values ?
我如何获得文本框值?
回答by TJ.
I'm using the index to access the four text fields. Also added the $fieldset as a context.
我正在使用索引访问四个文本字段。还添加了 $fieldset 作为上下文。
$("#tableId").find('tr fieldset').each(function (i) {
var $fieldset = $(this);
FirstName =FirstName +','+ $('input:text:eq(0)', $fieldset).val();
LastName =LastName +','+ $('input:text:eq(1)', $fieldset).val();
Email =Email +','+ $('input:text:eq(2)', $fieldset).val();
PhoneNumber = PhoneNumber+','+ $('input:text:eq(3)', $fieldset).val();
});
回答by Joseph
assuming that your table somewhat looks like this:
假设您的表有点像这样:
?<table>
<tr>
<td>
<input type="text" class="firstName" value="foo1">
<input type="text" class="lastName" value="ba1r">
</td>
</tr>
<tr>
<td>
<input type="text" class="firstName" value="foo2">
<input type="text" class="lastName" value="bar2">
</td>
</tr>
</table??????????????????????????>????
here's a snippetthat might work (may need to be modified to your needs)
$(function() {
//storage
var data = {
firstName : [], lastName : []
}
//loop through the tr's
$('table tr').each(function() {
//look for the fields firstName and lastName in the tr
//get their values and push into storage
data.firstName.push($('.firstName', this).val());
data.lastName.push($('.lastName', this).val());
});
//view the data in the console
console.log(data);
//if you prefer the comma separated data
var firstName = data.firstName.join(',');
var lastName = data.lastName.join(',');
});?
回答by Jignesh Rajput
try to get value using like this
尝试使用这样的方法获得价值
$(this).find("input[id^='txtFirstName']").val()
回答by Manatok
$(this)
inside the loop will give you a handle on the current element
$(this)
在循环内部会给你一个当前元素的句柄
回答by JustinN
You could use a selector like:
您可以使用如下选择器:
$("input[type='input']")
Or apply a specific class
to the textboxes
which one you want and search for that.
或者将特定内容class
应用于textboxes
您想要的内容并进行搜索。
回答by Th0rndike
The ideal solution to this is to allow you to get the rows by id even if a row is deleted. Do this by updating the id of the rows every time a row gets deleted.
对此的理想解决方案是允许您按 id 获取行,即使行被删除。通过在每次删除行时更新行的 id 来做到这一点。