如何使用enter将光标移动到下一个文本框而不是提交?

时间:2022-12-06 22:10:15

screenshot I'm trying to move the cursor to the next textbox by pressing Enter.

截图我正在尝试按Enter键将光标移动到下一个文本框。

Here is my aspx code:

这是我的aspx代码:

<strong>
                    <asp:Label ID="bondtapelbl" runat="server" Text="Bond Tape :" Font-Bold="True" Font-Italic="False" Font-Names="Arial Black" Font-Size="Medium"></asp:Label>
                </strong>
                <asp:TextBox ID="bondtape" runat="server" BackColor="#CCCCCC" Height="35px" Font-Size="Medium" Width="130px" CssClass="bt" />

                &nbsp;<span class="auto-style6">&amp;</span><strong>
                    <asp:Label ID="productranklbl" runat="server" Text="Product Rank :" Font-Bold="True" Font-Italic="False" Font-Names="Arial Black" Font-Size="Medium"></asp:Label>

                    <asp:TextBox ID="productrank" runat="server" BackColor="#CCCCCC" Height="35px" Font-Size="Medium" Width="130px" CssClass="pr" />

                    <asp:Button ID="bontapeButton" Text="Search" runat="server" BackColor="#3399FF" BorderStyle="None" CssClass="btsearch" ForeColor="White" Height="39px" Width="80px" />
                </strong></td>

I'm using javascript for this. The problem is that instead of moving the cursor to the next text box, it automatically execute the search button.

我正在使用javascript。问题是,它不是将光标移动到下一个文本框,而是自动执行搜索按钮。

   <script type="text/javascript">

                //Bind keyup event to textbox
                $('btsearch[type="bondtape"]').keyup(function (event) {
                    if (e.keyCode == 13) {
                        $("pr").next().focus();
                    }
                });

            </script>

My javascript is based on this answer that I got from the other forum:

我的javascript基于我从其他论坛得到的答案:

$('input[type="textbox"]').keyup(function(e) {
if(e.keyCode == 13) {
    $(this).next().focus();
}});

I've only learnt javascript for 2/3 days so I don't really know in which part I'm wrong. I would be grateful if anyone could help me. Thanks in advance.

我只学习了2/3天的javascript,所以我真的不知道哪个部分我错了。如果有人能帮助我,我将不胜感激。提前致谢。

1 个解决方案

#1


0  

okay I just want to share the answer that I got. Here is the link that I got my answer from(you can also try the demo) : http://www.latentmotion.com/downloads/enter-to-tab.html
and below is the javascript that work with my project. The solution that I got is I replaced my javascript on the question with the javascript below. I hope this could help other people .

好吧我只是想分享我得到的答案。这是我得到答案的链接(您也可以尝试演示):http://www.latentmotion.com/downloads/enter-to-tab.html及以下是与我的项目一起使用的JavaScript。我得到的解决方案是我用下面的javascript替换了我的问题javascript。我希望这可以帮助其他人。

 $(document).ready(function(){
        $("input").not( $(":button") ).keypress(function (evt) {
            if (evt.keyCode == 13) {
                iname = $(this).val();
                if (iname !== 'Submit'){    
                    var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select');
                    var index = fields.index( this );
                    if ( index > -1 && ( index + 1 ) < fields.length ) {
                        fields.eq( index + 1 ).focus();
                    }
                    return false;
                }
            }
        });
    });

#1


0  

okay I just want to share the answer that I got. Here is the link that I got my answer from(you can also try the demo) : http://www.latentmotion.com/downloads/enter-to-tab.html
and below is the javascript that work with my project. The solution that I got is I replaced my javascript on the question with the javascript below. I hope this could help other people .

好吧我只是想分享我得到的答案。这是我得到答案的链接(您也可以尝试演示):http://www.latentmotion.com/downloads/enter-to-tab.html及以下是与我的项目一起使用的JavaScript。我得到的解决方案是我用下面的javascript替换了我的问题javascript。我希望这可以帮助其他人。

 $(document).ready(function(){
        $("input").not( $(":button") ).keypress(function (evt) {
            if (evt.keyCode == 13) {
                iname = $(this).val();
                if (iname !== 'Submit'){    
                    var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select');
                    var index = fields.index( this );
                    if ( index > -1 && ( index + 1 ) < fields.length ) {
                        fields.eq( index + 1 ).focus();
                    }
                    return false;
                }
            }
        });
    });