2013-10-26 71 views
0

我在我的html頁面中有一個提交按鈕和搜索按鈕。當我點擊提交它去適當的腳本。現在我在html中有一個搜索按鈕,它也會檢查驗證。我不希望搜索按鈕具有所有這些驗證,只需要在單擊搜索按鈕時在該文本字段中的任何一箇中輸入數據。我該如何使用JavaScript?如何使用兩個提交按鈕清除驗證問題?

<!DOCTYPE html> 
<html> 
<head> 
    <title>INFORMATION</title> 
<script type="text/javascript"> 

function validate() { 

    if (document.myForm.firstname.value==""){ 
    alert("Please provide the firstname"); 
    return false; 
    } 

    if (document.myForm.lastname.value==""){ 
    alert("Please provide the lastname"); 
    return false; 
    } 

    if (document.myForm.age.value=="" || isNaN(document.myForm.age.value) ||  document.myForm.age.value<1){ 
    alert("Please provide the valid age in Integer"); 
    return false; 
    } 

    if (document.myForm.gender.value==""){ 
    alert("Please provide the gender"); 
    return false; 
    } 

}  
</script> 
</head> 
    <body> 
     <form action = "/~neeraj/cgi-bin/test.py" name="myForm" method="post"  onsubmit="return(validate())"> 
      <table cellspacing="3" cellpadding="2" border="1"> 
       <tr> 
        <td> FirstName:* </td> 
        <td> <input type="text" name="firstname" /></td> 
       </tr> 

       <tr> 
        <td>LastName:* </td> 
        <td> <input type="text" name="lastname" /></td> 
       </tr> 

       <tr> 
        <td>Age:* </td> 
        <td><input type="text" name="age" /></td> 
       </tr> 

       <tr> 
        <td>Gender:* </td> 
        <td><input type="radio" name="gender" value="male" /> Male 
         <input type="radio" name="gender" value="female" /> Female</td> 
       </tr> 
      </table> 

       <br> 
       <input type="submit" name="submit "value="SUBMIT"> 
       <input type="reset" name="reset" value="RESET"> 
       <input type="submit" name="search" formmethod="post"  formaction="/~neeraj/cgi-bin/search.py" value="SEARCH"> 
     </form> 

    </body> 
</html> 
+0

點擊搜索按鈕,當你想要什麼? – yilmazburk

+0

如果搜索按鈕被點擊,它應該跳轉到另一個腳本,繞過所有的驗證 –

回答

0
<!DOCTYPE html> 
<html> 
    <head> 
    <title>INFORMATION</title> 
    <script type="text/javascript"> 

function validate() 
{ 
if (document.myForm.firstname.value=="") 
{ 
alert("Please provide the firstname"); 
return false; 
} 
else if (document.myForm.lastname.value=="") 
{ 
alert("Please provide the lastname"); 
return false; 
} 
else if (document.myForm.age.value=="" || isNaN(document.myForm.age.value) ||  document.myForm.age.value<1) 
{ 
alert("Please provide the valid age in Integer"); 
return false; 
} 
else if (document.myForm.gender.value=="") 
{ 
alert("Please provide the gender"); 
return false; 
} 
}  
</script> 
</head> 
<body> 
    <form action = "/~neeraj/cgi-bin/test.py" name="myForm" method="post"> 
     <table cellspacing="3" cellpadding="2" border="1"> 
      <tr> 
       <td> FirstName:* </td> 
       <td> <input type="text" name="firstname" /></td> 
      </tr> 

      <tr> 
       <td>LastName:* </td> 
       <td> <input type="text" name="lastname" /></td> 
      </tr> 

      <tr> 
       <td>Age:* </td> 
       <td><input type="text" name="age" /></td> 
      </tr> 

      <tr> 
       <td>Gender:* </td> 
       <td><input type="radio" name="gender" value="male" /> Male 
        <input type="radio" name="gender" value="female" /> Female</td> 
      </tr> 
     </table> 

      <br> 
      <input type="submit" name="submit1" value="SUBMIT" onclick="return(validate())"> 
      <input type="reset" name="reset1" value="RESET"> 
      <input type="submit" name="search" value="SEARCH"> 
    </form> 
    </body> 
</html> 

按鈕onclick="return(validate())"使用onclick。

+0

我希望當點擊搜索它應該去另一個腳本,不應該驗證所有文本字段 –

+0

我想單擊提交時驗證,並且沒有驗證對於單擊搜索時提交的文本 –

+0

如果您需要另一個搜索驗證,則必須創建新的js函數。在搜索調用該功能。 – Prateek

0

只要改變如下片段:

<form action = "/~neeraj/cgi-bin/test.py" name="myForm" method="post"> 
    : 
<input type="submit" name="submit" value="ADD" onclick="return(validate())"> 
<input type="reset" name="reset" value="RESET">       
<input type="submit" name="search" formmethod="post" formaction="/~neeraj/cgi-bin /search.py" value="SEARCH">