簡體   English   中英

頁面加載時如何關注輸入框?

[英]How to focus on input box when page loads?

我試圖在頁面首次加載時專注於頁面上的第一個文本框或輸入框。 因此,當頁面加載時,我希望在第一個文本框中顯示一個閃爍的光標。 我在Google上搜索了無數解決方案,所有的解決方案都產生了相同的結果:使用.focus()協議。 我已經嘗試過,但仍然無法正常工作。

這是在我的init函數中:

$(document).ready(function () {
                $('#TranslationTextBox:first').focus();
                $(".parametersNumericbox:first").focus();
            });

            $(".parametersNumericbox").focus(function () {
                var input = $(this);
                setTimeout(function () {
                    input.select();
                });
            });

只有當我多次單擊時,它才帶我到第一個文本框。

編輯:html代碼(使用Kendo UI):

 <td id="TranslationTextBox">
                            @(Html.Kendo().NumericTextBoxFor<double>(model => model.Translation).Format("#").Decimals(0).Min(0)
                                  .HtmlAttributes(new { @class = "parametersNumericbox" })
                                  .Events(e => e
                                      .Change("TranslationTextBoxChanged")
                                      ))
                        </td>

我當前遇到的問題是由於某種原因,焦點似乎出現在頁面上文本字段之前的按鈕上。

當然,您可以使用Javascript,但也可以使用HTML:

<input type="text" autofocus>

在jQuery中,您可以執行以下操作:

$("input, textarea, select").filter(":first").focus();

您的代碼似乎有一個問題,您正在關注多個項目。 當您這樣做時,最后聚焦的項目將獲得焦點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM