2015-05-20 27 views
1

需要一些幫助,在這一個...窗體操作和Javascript衝突

我有一種形式,一個腳本來顯示警報的工作...

我使用這個代碼來實現這一其做工精細:

 <input id="autocomplete" class="input_message" type="text" name="message" autocomplete="off" placeholder="...type your message" ><br/>        
     <input id="input_send" class="input_send" type="submit" name="send" value="SEND"> 

,但如果我把某種形式的方法,在我的腳本的警報不工作..

<form enctype="multipart/form-data" method="post"> 
     <input id="autocomplete" class="input_message" type="text" name="message" autocomplete="off" placeholder="...type your message" ><br/>        
     <input id="input_send" class="input_send" type="submit" name="send" value="SEND"> 
    </form> 

這裏是我的腳本:

<div id="alertbox"> 
<script> 
$(document).ready(function(){ 
    $('#input_send').click(function(){ 
     var post = $('#autocomplete').val(); 
     if(post==""){ 
      alert('Enter Message Please'); 
     }else{ 
     $('#loader').fadeIn(400).html('<img src="./loader.gif" align="absmiddle">&nbsp;<span class="loading">sending</span>'); 
     var datasend = "alert"; 
     $.ajax({ 
      type:'post', 
      url:'./includes/alerts.php', 
      data:datasend, 
      cache:false, 
      success:function(msg__){  
       $('#autocomplete').val(''); 
       $('#loader').hide(); 
       $('#alertbox').fadeIn('slow').prepend(msg__); 
       $('#alerts').delay(5000).fadeOut('slow'); 
      } 
     }); 
     } 
    })   
}); 
</script> 
</div> 

在此先感謝..

+0

它可能正好與我一樣工作當你點擊表單中的提交按鈕時,表單會被提交,重新加載頁面? – adeneo

+0

是的,但我的警報腳本沒有顯示。 – Ryewell

+0

你不應該按下按鈕的點擊事件。您應該在表單上處理提交事件。你也可以在那裏提交提交。 –

回答

0

提交按鈕後表單到服務器,並重新加載頁面;

您可以添加AJAX功能後,返回false ;, ,那麼是不是發送給服務器

+0

不工作.. :( – Ryewell

0

需要防止表單提交

$(document).ready(function(){ 
    $('#input_send').click(function(e){ 
     e.preventDefault();//here 
     var post = $('#autocomplete').val(); 
     if(post==""){ 
      alert('Enter Message Please'); 
     }else{ 
     $('#loader').fadeIn(400).html('<img src="./loader.gif" align="absmiddle">&nbsp;<span class="loading">sending</span>'); 
     var datasend = "alert"; 
     $.ajax({ 
      type:'post', 
      url:'./includes/alerts.php', 
      data:datasend, 
      cache:false, 
      success:function(msg__){  
       $('#autocomplete').val(''); 
       $('#loader').hide(); 
       $('#alertbox').fadeIn('slow').prepend(msg__); 
       $('#alerts').delay(5000).fadeOut('slow'); 
      } 
     }); 
     } 
    })   
}); 

或者從submit改變#input_send的類型button這樣表單提交將不會被觸發

<input id="input_send" class="input_send" type="button" name="send" value="SEND"> 
+0

謝謝,但如果我改變#input_send從提交到按鈕..警報正在工作,但在另一邊..我的表單不工作,無法獲得一些數據.. – Ryewell