2013-05-27 20 views
0

我有一個表單元素,裏面有一些內容,如下所示。POST方法甚至在if條件失敗的情況下工作

<form action="insertserverdata.php" id="toPopup"> 
    <table> 
    <tr> 
     <td>IP address:</td> 
     <td><input type="text" id="ip" /></td> 
    </tr> 
    <tr> 
     <td>Port:</td> 
     <td><input type="text" id="port" /></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td> 
      <input type="submit"/> 
     </td> 
    </tr> 
    </table> 
</form> 

和下面的jQuery代碼。

$("#toPopup").submit(function(event){ 
    if($('#ip').val()=="") { 
     alert("IP field is Empty"); 
    }else if($('#port').val()=="") { 
     alert("Port field is Empty"); 
    }else { 
     //else code to be executed. 
    } 
}); 

此else else if的最後一個else塊包含將數據發佈到insertserverdata.php的代碼。我的意圖是隻有在兩個文本字段填充了一些數據時才重定向到insertserverdata.php。但是,當我點擊提交按鈕的文本字段中沒有文本,如果jquery的塊將正常工作,之後,它將重定向到insertserverdata.php頁面,但我不想要那個。我需要什麼樣的變化來填充它? 。請幫助我的朋友。

+3

ip和端口具有相同的ID! – Class

+0

對不起親愛的。那是我的錯誤。我確實在抄襲它。我會編輯它。 –

回答

4

嘗試在每個作廢的支票返回

$("#toPopup").submit(function(event){ 
    if($('#ip').val()=="") { 
     alert("IP field is Empty"); 
     return false; 
    }else if($('#port').val()=="") { 
     alert("Port field is Empty"); 
     return false; 
    } 
    //Do the stuff 
}); 

還有一件事,在你的HTML

+0

這工作好花花公子:)謝謝:)會給予綠色刻度標記,因爲我不能做到15分鐘! –

+0

@ Gautam3164你也可以修剪IP和端口值...否則有時你會得到問題。 – sachin

+0

Thats Tru sachin。有時需要修剪數據。 –

2

嘗試改變的兩個文本框的ID爲「IP」和「端口」這,

$("#toPopup").submit(function(event){ 
    event.preventDefault(); 
    if($('#ip').val()=="") { 
     alert("IP field is Empty"); 
    }else if($('#port').val()=="") { 
     alert("Port field is Empty"); 
    }else { 
     //else code to be executed. 
     return true; 
    } 
    return false; 
}); 
0

請嘗試下面的應該顯示錯誤消息,然後返回true,而不是顯示一個或另一個錯誤消息,如果b其他都是空的。

$("#toPopup").submit(function(event){ 
    var errors = false; 
    if($.trim($('#ip').val())=="") { 
     alert("IP field is Empty"); 
     errors = true; 
    } 
    if($.trim($('#port').val())=="") { 
     alert("Port field is Empty"); 
     errors = true; 
    } 
    if(errors){ 
     return false; 
    }else{ 
     //else code to be executed. 
    } 
}); 
相關問題