[英]Move from one control to another in Listview using JavaScript
I have 4 textbox's in an asp Listview. 我在ASP Listview中有4个文本框。 I have added the Javascript below to allow me to be able to use the arrow keys to move from one tb to another.
我在下面添加了Javascript,使我能够使用箭头键从一个TB移到另一个。 The problem that I am having is that when I am at Textbox1 I CAN use the left arrow key to move to Textbox4, however I CAN NOT use the right arrow key to move from Textbox4 to Textbox1.
我遇到的问题是,当我在Textbox1上时,可以使用向左箭头键移至Textbox4,但是不能使用向右箭头键从Textbox4移至Textbox1。 See the image for reference.
请参阅图像以供参考。
<script type="text/javascript">
$('input[type="text"],textarea').keydown(function (e) {
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
if (key == 39) {
e.preventDefault();
var inputs = $(this).parents('form').find(':input[type="text"]:enabled:visible:not("disabled"),textarea');
inputs.eq(inputs.index(this) + 1).focus();
inputs.eq(inputs.index(this) + 1).click();
}
if (key == 37) {
e.preventDefault();
var inputs = $(this).parents('form').find(':input[type="text"]:enabled:visible:not("disabled"),textarea');
inputs.eq(inputs.index(this) - 1).focus();
inputs.eq(inputs.index(this) - 1).click();
}
});
</script>
I would wrap the 4 TextBoxes in an element and bind the left and right clicks to that elements TextBoxes. 我将4个TextBoxes包装在一个元素中,并将左右单击绑定到该元素TextBoxes。 Then use the jQuery
prevAll
and nextAll
. 然后使用jQuery
prevAll
和nextAll
。
<div id="4TextBox">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</div>
<script type="text/javascript">
$('#4TextBox input[type="text"]').keydown(function (e) {
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
if (key == 39) {
e.preventDefault();
$(this).nextAll('input[type="text"]').first().focus();
}
if (key == 37) {
e.preventDefault();
$(this).prevAll('input[type="text"]').first().focus();
}
});
</script>
However this only works if the TextBoxes are all next to each other. 但是,这仅在TextBoxes彼此相邻时才有效。 If you would wrap the TextBoxes with some other element like the sample below.
如果您将TextBoxes与其他元素包装在一起,例如下面的示例。 It will skip the 3rd TextBox.
它将跳过第三个文本框。 So depending on your html design you may need to tweak it some more.
因此,根据您的html设计,您可能需要对其进行一些调整。
<div id="4TextBox">
<asp:TextBox ID="TextBox1a" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<p><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></p>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.