简体   繁体   English

使用JavaScript从Listview中的一个控件移动到另一个控件

[英]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. 请参阅图像以供参考。

4 Textbox's 4文字框

<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 prevAllnextAll

<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.

相关问题 将房产从一个object移到另一个Javascript - Move a property from one object to another in Javascript Tableau JS过滤器:使用javascript API,将过滤器从一个仪表板移动到另一个仪表板 - Tableau JS filter: Using javascript API, move filters from one dashboard to another 如何使用纯JavaScript在DOM中将元素从一个地方移动到另一个地方? - How to move an element from one place to another in the DOM using plain JavaScript? 使用javascript或jquery将数据从一个表移动和删除到另一个表 - Move and delete data from one table to another table using javascript or jquery 如何使用javascript将内容从表格的一行移动到另一表格的行 - How to move content from one row of the table to another table row using javascript 将Javascript控件与ListView中的Checkbox一起使用 - Using Javascript control with Checkbox in a ListView Javascript:尝试将项目从一个数组随机移动到另一个数组 - Javascript: Trying to randomly move items from one array to another 在vanilla JavaScript 中将LI 从一个UL 移动到另一个UL? - Move LI to from one UL to another in vanilla JavaScript? (JavaScript和HTML)将项目从一个列表移动到另一个列表 - (JavaScript & HTML )Move items from one list to another Javascript:将对象从一个数组移动到另一个数组:最佳方法? - Javascript: move objects from one array to another: Best approach?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM