2013-06-22 57 views
1

我想根據他們的安排專注於文本框....我的問題是根據我的代碼它直接關注第三個文本框..但我想按照texbox的安排重點。 ..例如,如果第一個文本框爲空,然後將重點放在第一個文本框,目前它的重點是第三個文本框...專注於使用javascript的文本框

<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

當你做檢查這種方式,你需要else 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"; 
       } 
+0

它爲我工作...謝謝你... –

0

我想你的情況下所有的文本框都是空白的& y ou在其他if語句中不使用else,因此重點在最後/第三個。請添加其他&如果其他情況,如:

<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> 
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"; 
        } } 

在上面的代碼,如果所有的文本框爲空,將繼續評估,如果條件從上到下,最後第三文本框將接收焦點。您需要像其他人所建議的那樣使用else if