2012-02-28 7 views
0

テキストボックスをスキップするとアラートが表示されますが、ページ上をクリックするとアラートも表示されます。フォーカスがテキストボックスに表示されるときに、どのように警告を表示できますか?新しいタブを開いても警告が表示されます。これを解決する方法はありますか?どうもありがとう。フォーカスがテキストボックス内にあるときに警告

$(function() { 
    $("input").blur(function() { 
    if(!this.value) { 
     alert("Text box " + ($(this).index() + 1) + " cannot be empty."); 
     this.focus(); //Keep focus on this input 
    } 
}); 

本体部

<input type="text" id="textbox1"/> 
<input type="text" id="textbox2"/> 
<input type="text" id="textbox3"/> 
<input type="text" id="textbox4"/> 

答えて

2
$(function() { 
     $('input:text').blur(function() { 
if(!this.value) { 
    alert("Text box " + ($(this).index() + 1) + " cannot be empty."); 
    this.focus(); //Keep focus on this input 
} 
}); 
}); 

http://api.jquery.com/text-selector/

を訪れることができます願っています。

+0

申し訳ありません、同じ問題が再び – saroj

+0

を発生した$置き換える。( '入力:テキストを')$( '入力[タイプ=テキストで(機能() をぼかします] ')。blur(function() –

0
<script type="text/javascript"> 
     function getAlert(txt) { 
       if (!txt.value) { 
        alert("Text box cannot be empty."); 
        txt.focus(); //Keep focus on this input 
       } 

     }; 
    </script> 

<input type="text" id="textbox1" onfocus="getAlert(this)"/> 

この問題が解決したことを願います。

+0

このテキストボックスにのみ焦点を当てると、ページの総掛けにつながる – saroj

0

$(function() { 
    $("input[type=text]").blur(function() { 
     if (!this.value && $(document.activeElement).attr("type") == "text") 
      alert("Text box " + ($(this).index() + 1) + " cannot be empty."); 
      this.focus(); 
     } 
    }); 
}); 

document.activeElementはあなたに現在選択されている要素を取得し、選択された要素がテキストボックスであるならば、あなたは比較することができ、これを試してみてください。このことができます

希望.........

関連する問題