jquery get a textbox in a table
I would like to select a textbox being dynamically created by an asp.net server control (listview) For each row in the table I need to run some javascript functionality over it.
Below I am trying to select the textbox based off of the name haveing the text. Is there a "better" way of doing this?
$('#tblNewAttendees tr').each(function() {
var test = $(this).find('input[name$="txtFirstName"]').val();
});
}
Html code
<table>
<thead>
</thead>
<tbody>
<tr>
<td>
<asp:TextBox ID="txtFirstName" Text='<%#Eval("FirstName")%>'
runat="server" Width="80px" />
</td>
</tr>
&开发者_StackOverflow中文版lt;/tobdy>
</table>
Assign a specific CSS class to all those text boxes, and then use class selectors to select those.
Suppose you have assigned myClass
as CSS class value. Then use the following to select them -
$('#tblNewAttendees tr').each(function()
{
var test = $(this).find('input.myClass').val();
});
The reason is, as far as I remember, the names that you assign to a text box is replaced by ASP.NET with something new after it parses the document. Suppose you have a text box whose name is myBox
. Then after the page is parsed, its name may become something like ct_1001_myBox_123
or something. So it's better to use a CSS class to select them in the client side.
You could do the following to simplify it (no need to do a .each() then a find within that) :
$('#tblNewAttendees tr input[name$="txtFirstName"]').each(function() {
var test = $(this).val();
}
You may want to change your markup to give the asp textbox a class and select based on the class name, since .net will rename the ID property.
You could give your textbox a unique class name and use the class name as the selector.
<asp:TextBox ID="txtFirstName" class="myClass" Text='<%#Eval("FirstName")%>' runat="server" />
Then in your script just select by the class
$('.myClass').each(function() {
var test = $(this).val();
});
}
精彩评论