使用javascript专注于文本框

时间:2013-06-22 08:18:38

标签: javascript jquery html

我想根据他们的安排专注于文本框....我的问题是根据我的代码它直接关注第三个文本框..但我想根据texbox的安排重点...例如,如果第一文本框是空白然后它将集中在第一个文本框,目前它专注于第三个文本框......

<input type="text" id="txtFirst"/>
           <input type="text" id="txtSecond"/>
            <input type="text" id="txtThird"/>
            <input type="button" onclick="check()" value="Check" />
            <script>
                function check() {
                    var err = "";
                    var first = document.getElementById("txtFirst");
                    var second = document.getElementById("txtSecond");
                    var third = document.getElementById("txtThird");
                    if (first.value == "") {
                        first.focus();
                        err = err + "enter the first value";
                    } if (second.value == "") {
                        second.focus();
                        err = err + "enter the second value";
                    } if (third.value == "") {
                        third.focus();
                        err = err + "enter the third value";
                    } }
                </script>

3 个答案:

答案 0 :(得分:3)

当你以这种方式进行检查时,你还需要if语句......

                if (first.value == "") {
                    first.focus();
                    err = err + "enter the first value";
                } else if (second.value == "") {
                    second.focus();
                    err = err + "enter the second value";
                } else if (third.value == "") {
                    third.focus();
                    err = err + "enter the third value";
                }

答案 1 :(得分:0)

我认为在您的情况下,所有文本框都是空白的&amp;你没有在其他if语句中使用else,因此重点是最后/第三个。请添加其他&amp;其他情况如:

<input type="text" id="txtFirst"/>
           <input type="text" id="txtSecond"/>
            <input type="text" id="txtThird"/>
            <input type="button" onclick="check()" value="Check" />
            <script>
                function check() {
                    var err = "";
                    var first = document.getElementById("txtFirst");
                    var second = document.getElementById("txtSecond");
                    var third = document.getElementById("txtThird");
                    if (first.value == "") {
                        first.focus();
                        err = err + "enter the first value";
                    } else if (second.value == "") {
                        second.focus();
                        err = err + "enter the second value";
                    } else if (third.value == "") {
                        third.focus();
                        err = err + "enter the third value";
                    } }
                </script>

答案 2 :(得分:0)

if (first.value == "") {
                        first.focus();
                        err = err + "enter the first value";
                    } if (second.value == "") {
                        second.focus();
                        err = err + "enter the second value";
                    } if (third.value == "") {
                        third.focus();
                        err = err + "enter the third value";
                    } }

在上面的代码中,如果所有文本框都为空,它将继续从上到下评估if条件,最后第三个文本框将获得焦点。您需要像其他人建议的那样使用else if