2011-12-08 23 views
0

我的html值:我想顯示的消息每場1次有使用jQuery

<html> 
    <body> 
    <input type="text" id="name" name="name" placeholder="First Name" /> 
    <label for="name">First Name </label> 
    <button id="submit" src="images/button.png" width="100" >Submit</button> 
    </body> 
</html> 

我的腳本是:

<script type="text/javascript" src="jquery-1.5.2.min.js"></script> 
<script type="text/javascript">           
    $(document).ready(function(){ 
    $('#submit').click(function() { 
     var emptyTextBoxes = $('input[type=text],[type=password]').filter(function() { return this.value == ""; 
    }); 
    emptyTextBoxes.each(function() { 
     $(emptyTextBoxes).nextAll('label').css({'display':'inline','color':'#F00'}); 
    }); 
    }); 

這裏有一次,我在我想顯示消息的文本字段中輸入值(表單提交)如何把這個提醒信息在這裏

+0

你想把這條信息放在哪裏?爲什麼沒有表單標籤?你打算如何在沒有表單標籤的情況下提交表單?這麼多的問題.... – ManseUK

回答

1

完全重新工作的版本,把你的輸入字段放到表單中,並糾正原來的javascript中的錯誤...

<html> 
    <head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function(){ 
     // use the id of the form (moved from the button) 
     $('#submit').bind('submit',function(){ 
      // I added `input` in front of your password selector to make it more accureate/efficient 
      var emptyTextBoxes = $('input[type=text],input[type=password]').filter(function(){ 
      return this.value == ""; 
      }); 
      // if there are no empty boxes 
      if(!emptyTextBoxes.length){ 
      alert('We finally did it') 
      } 
      // this prevents the form from being submitted 
      return false; 
     }) 
     }) 
    </script> 
    </head> 
    <body> 
    <!-- wrap with form and give form id --> 
    <form action='#' method='POST' id="submit"> 
     <input type="text" id="name" name="name" placeholder="First Name" /> 
     <label for="name">First Name </label> 
     <!-- replace button with submit input --> 
     <input type='submit' width="100" /> 
    </form> 
    </body> 
</html> 
+0

我試過了不起作用 –

+0

提交按鈕不會觸發提交事件,表單確實如此。 –

+0

即表示沒有表單,因此沒有提交事件可以被解僱。 –

1

基於什麼比利月發佈,但正確的事件

$('#submit').click(function(event) { 

    var emptyTextBoxes = $('input[type=text],[type=password]').filter(function() { return this.value == ""; }); 

    if(emptyTextBoxes.length < 1) { 
     alert('Well done, you did it!!'); 
    } else { 
     // ignore the click if all fields haven't been clicked 
     event.preventDefault(); 
    } 

}); 

Working example,原代碼有一定的誤差。

+0

嘿它的工作感謝:) –

+0

不用擔心,如上所述...基於比利月亮的答案,只是調整了解決了一些問題。 –

相關問題