我有大约60个asp.net文本框,每个文本框都有一个类似于"InputTextBox##“的ID。
如何使用JQuery跳转到用户可以跳转到下一个文本框并自动填充上一个文本框的值的位置?
我试过了:
$("input[id*='Input']").focusin(function () {
var $this = $(this);
if ($this.val() === "") {
$this.val($this.prevAll("input[id*='Input']").last().val() || $this.attr('value'))
}
});但这没有任何作用。我试图通过在id中搜索"Input“来选择焦点上的文本框的id,但一无所获。
编辑:以下是我的一些html,让您了解我正在使用的内容:
<asp:TableRow>
<asp:TableCell><asp:Label ID="InputLabel2" runat="server" Text="2">
</asp:Label></asp:TableCell>
<asp:TableCell><asp:TextBox ID="InputTextBox2" runat="server" Width="70px" >
</asp:TextBox>
<asp:TextBox ID="InputTextBox2Y" runat="server" Width="35px" Visible="False"
Enabled="False"></asp:TextBox>
</asp:TableCell>
<asp:TableCell><asp:Label ID="InputLabel12" runat="server" Text="12">
</asp:Label></asp:TableCell>
<asp:TableCell><asp:TextBox ID="InputTextBox12" runat="server" Width="70px">
</asp:TextBox>
<asp:TextBox ID="InputTextBox12Y" runat="server" Width="35px"
Visible="False" Enabled="False"></asp:TextBox>
</asp:TableCell>
<asp:TableCell><asp:Label ID="InputLabel22" runat="server" Text="22">
</asp:Label></asp:TableCell>
<asp:TableCell><asp:TextBox ID="InputTextBox22" runat="server" Width="70px">
</asp:TextBox>
<asp:TextBox ID="InputTextBox22Y" runat="server" Width="35px"
Visible="False" Enabled="False"></asp:TextBox>
</asp:TableCell>
<asp:TableCell><asp:Label ID="InputLabel32" runat="server" Text="32">
</asp:Label></asp:TableCell>
<asp:TableCell><asp:TextBox ID="InputTextBox32" runat="server" Width="70px">
</asp:TextBox>
<asp:TextBox ID="InputTextBox32Y" runat="server" Width="35px"
Visible="False" Enabled="False"></asp:TextBox>
</asp:TableCell>
<asp:TableCell><asp:Label ID="InputLabel42" runat="server" Text="42">
</asp:Label></asp:TableCell>
<asp:TableCell><asp:TextBox ID="InputTextBox42" runat="server" Width="70px">
</asp:TextBox>
<asp:TextBox ID="InputTextBox42Y" runat="server" Width="35px"
Visible="False" Enabled="False"></asp:TextBox>
</asp:TableCell>
<asp:TableCell><asp:Label ID="InputLabel52" runat="server" Text="52">
</asp:Label></asp:TableCell>
<asp:TableCell><asp:TextBox ID="InputTextBox52" runat="server" Width="70px">
</asp:TextBox>
<asp:TextBox ID="InputTextBox52Y" runat="server" Width="35px"
Visible="False" Enabled="False"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>下面是该表的图像:http://imgur.com/a/hu3Op
发布于 2017-08-25 11:50:48
如果需要在水平方向上使用上一次输入的值自动填充输入:
$(document).on('focus', 'input[id*=InputTextBox]', function() {
if ($(this).val() == "") {
var tdIndex = $(this).closest('td').index();
if ((tdIndex - 2) >= 0) {
var prevTd = $(this).closest('tr').find('td').eq(tdIndex - 2);
if (prevTd != null && prevTd != undefined) {
$(this).val($(prevTd).find('input[id*=InputTextBox]').val());
}
}
}
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td><span id="MainContent_InputLabel1">1</span></td>
<td><input name="ctl00$MainContent$InputTextBox1" type="text" id="MainContent_InputTextBox1" style="width:70px;"></td>
<td><span id="MainContent_InputLabel11">11</span></td>
<td><input name="ctl00$MainContent$InputTextBox11" type="text" id="MainContent_InputTextBox11" style="width:70px;"></td>
<td><span id="MainContent_InputLabel21">21</span></td>
<td><input name="ctl00$MainContent$InputTextBox21" type="text" id="MainContent_InputTextBox21" style="width:70px;"></td>
<td><span id="MainContent_InputLabel31">31</span></td>
<td><input name="ctl00$MainContent$InputTextBox31" type="text" id="MainContent_InputTextBox31" style="width:70px;"></td>
<td><span id="MainContent_InputLabel41">41</span></td>
<td><input name="ctl00$MainContent$InputTextBox41" type="text" id="MainContent_InputTextBox41" style="width:70px;"></td>
<td><span id="MainContent_InputLabel51">51</span></td>
<td><input name="ctl00$MainContent$InputTextBox51" type="text" id="MainContent_InputTextBox51" style="width:70px;"></td>
</tr>
<tr>
<td><span id="MainContent_InputLabel2">2</span></td>
<td><input name="ctl00$MainContent$InputTextBox2" type="text" id="MainContent_InputTextBox2" style="width:70px;"></td>
<td><span id="MainContent_InputLabel12">12</span></td>
<td><input name="ctl00$MainContent$InputTextBox12" type="text" id="MainContent_InputTextBox12" style="width:70px;"></td>
<td><span id="MainContent_InputLabel22">22</span></td>
<td><input name="ctl00$MainContent$InputTextBox22" type="text" id="MainContent_InputTextBox22" style="width:70px;"></td>
<td><span id="MainContent_InputLabel32">32</span></td>
<td><input name="ctl00$MainContent$InputTextBox32" type="text" id="MainContent_InputTextBox32" style="width:70px;"></td>
<td><span id="MainContent_InputLabel42">42</span></td>
<td><input name="ctl00$MainContent$InputTextBox42" type="text" id="MainContent_InputTextBox42" style="width:70px;"></td>
<td><span id="MainContent_InputLabel52">52</span></td>
<td><input name="ctl00$MainContent$InputTextBox52" type="text" id="MainContent_InputTextBox52" style="width:70px;"></td>
</tr>
</tbody>
</table>
如果您需要在垂直基础上使用上一个输入的值自动填充输入:
$(document).on('focus', 'input[id*=InputTextBox]', function() {
if ($(this).val() == "") {
var tdIndex = $(this).closest('td').index();
var rowIndex = $(this).closest('tr').index();
if (rowIndex > 0) {
var prevRowCol = $(this).closest('table').find('tr').eq(rowIndex - 1).find('td').eq(tdIndex);
if (prevRowCol != null && prevRowCol != undefined) {
$(this).val($(prevRowCol).find('input[id*=InputTextBox]').val());
}
}
}
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td><span id="MainContent_InputLabel1">1</span></td>
<td><input name="ctl00$MainContent$InputTextBox1" type="text" id="MainContent_InputTextBox1" style="width:70px;"></td>
<td><span id="MainContent_InputLabel11">11</span></td>
<td><input name="ctl00$MainContent$InputTextBox11" type="text" id="MainContent_InputTextBox11" style="width:70px;"></td>
<td><span id="MainContent_InputLabel21">21</span></td>
<td><input name="ctl00$MainContent$InputTextBox21" type="text" id="MainContent_InputTextBox21" style="width:70px;"></td>
<td><span id="MainContent_InputLabel31">31</span></td>
<td><input name="ctl00$MainContent$InputTextBox31" type="text" id="MainContent_InputTextBox31" style="width:70px;"></td>
<td><span id="MainContent_InputLabel41">41</span></td>
<td><input name="ctl00$MainContent$InputTextBox41" type="text" id="MainContent_InputTextBox41" style="width:70px;"></td>
<td><span id="MainContent_InputLabel51">51</span></td>
<td><input name="ctl00$MainContent$InputTextBox51" type="text" id="MainContent_InputTextBox51" style="width:70px;"></td>
</tr>
<tr>
<td><span id="MainContent_InputLabel2">2</span></td>
<td><input name="ctl00$MainContent$InputTextBox2" type="text" id="MainContent_InputTextBox2" style="width:70px;"></td>
<td><span id="MainContent_InputLabel12">12</span></td>
<td><input name="ctl00$MainContent$InputTextBox12" type="text" id="MainContent_InputTextBox12" style="width:70px;"></td>
<td><span id="MainContent_InputLabel22">22</span></td>
<td><input name="ctl00$MainContent$InputTextBox22" type="text" id="MainContent_InputTextBox22" style="width:70px;"></td>
<td><span id="MainContent_InputLabel32">32</span></td>
<td><input name="ctl00$MainContent$InputTextBox32" type="text" id="MainContent_InputTextBox32" style="width:70px;"></td>
<td><span id="MainContent_InputLabel42">42</span></td>
<td><input name="ctl00$MainContent$InputTextBox42" type="text" id="MainContent_InputTextBox42" style="width:70px;"></td>
<td><span id="MainContent_InputLabel52">52</span></td>
<td><input name="ctl00$MainContent$InputTextBox52" type="text" id="MainContent_InputTextBox52" style="width:70px;"></td>
</tr>
</tbody>
</table>如果您需要使用前一个输入的值自动填充输入,而不考虑垂直和水平方向:
$(document).on('focus', 'input[id*=InputTextBox]', function() {
if ($(this).val() == "") {
var tdIndex = $('table td').index($(this).closest('td'));
if (tdIndex - 2 > 0) {
var prevTD = $('table td').eq(tdIndex - 2);
$(this).val($(prevTD).find('input[id*=InputTextBox]').val());
}
}
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td><span id="MainContent_InputLabel1">1</span></td>
<td><input name="ctl00$MainContent$InputTextBox1" type="text" id="MainContent_InputTextBox1" style="width:70px;"></td>
<td><span id="MainContent_InputLabel11">11</span></td>
<td><input name="ctl00$MainContent$InputTextBox11" type="text" id="MainContent_InputTextBox11" style="width:70px;"></td>
<td><span id="MainContent_InputLabel21">21</span></td>
<td><input name="ctl00$MainContent$InputTextBox21" type="text" id="MainContent_InputTextBox21" style="width:70px;"></td>
<td><span id="MainContent_InputLabel31">31</span></td>
<td><input name="ctl00$MainContent$InputTextBox31" type="text" id="MainContent_InputTextBox31" style="width:70px;"></td>
<td><span id="MainContent_InputLabel41">41</span></td>
<td><input name="ctl00$MainContent$InputTextBox41" type="text" id="MainContent_InputTextBox41" style="width:70px;"></td>
<td><span id="MainContent_InputLabel51">51</span></td>
<td><input name="ctl00$MainContent$InputTextBox51" type="text" id="MainContent_InputTextBox51" style="width:70px;"></td>
</tr>
<tr>
<td><span id="MainContent_InputLabel2">2</span></td>
<td><input name="ctl00$MainContent$InputTextBox2" type="text" id="MainContent_InputTextBox2" style="width:70px;"></td>
<td><span id="MainContent_InputLabel12">12</span></td>
<td><input name="ctl00$MainContent$InputTextBox12" type="text" id="MainContent_InputTextBox12" style="width:70px;"></td>
<td><span id="MainContent_InputLabel22">22</span></td>
<td><input name="ctl00$MainContent$InputTextBox22" type="text" id="MainContent_InputTextBox22" style="width:70px;"></td>
<td><span id="MainContent_InputLabel32">32</span></td>
<td><input name="ctl00$MainContent$InputTextBox32" type="text" id="MainContent_InputTextBox32" style="width:70px;"></td>
<td><span id="MainContent_InputLabel42">42</span></td>
<td><input name="ctl00$MainContent$InputTextBox42" type="text" id="MainContent_InputTextBox42" style="width:70px;"></td>
<td><span id="MainContent_InputLabel52">52</span></td>
<td><input name="ctl00$MainContent$InputTextBox52" type="text" id="MainContent_InputTextBox52" style="width:70px;"></td>
</tr>
</tbody>
</table>
在给定的示例中,您有两个相邻的文本框,其中一个对服务器端本身是隐藏的。如果您随时启用它,则需要将tdIndex - 2更改为tdIndex - 3,因为将呈现一个新列。
https://stackoverflow.com/questions/45869328
复制相似问题