2016-08-24 163 views
0

我其實需要幫助來糾正我的代碼。我觀看了YouTube上的視頻,但在該視頻中,他使用複選框而不是按鈕。所以我有問題的JavaScript的if語句。我已經檢查了以前的帖子有同樣的問題,但找不到可以解決我的問題的帖子。請幫幫我。非常感謝。如何使用javascript單擊按鈕時隱藏/顯示錶單?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Untitled Document</title> 
    <link href="css/tabMenu.css" rel="stylesheet" type="text/css" /> 

    <style> 
     .hidden { 
      display:none;} 
    </style> 
    <script type="text/javascript"> 
     function showHide() 
     { 
      var button = document.getElementById("butt"); 
      var hiddeninput = document.getElementByClassName("hidden"); 

       if(button.click) 
       { 
        hiddeninput.style.display = "block"; 
       } 
       else 
       { 
        hiddeninput.style.display = "none"; 
       } 
     } 

    </script> 

    </head> 

    <body> 
    <div id="navbar"> 
    <div id="holder"> 

    <ul> 
    <li><a href="employee.php" id="onlink">Employer Database</a></li> 
    <li><a href="companypresence.php">Company Presence</a></li> 
    <li><a href="companypromotion.php">Company Promotion</a></li> 
    </ul> 



    </div><!--end of holder div --> 
    </div><!--end of navbar div --> 

    </br> 

    <p id="p1"><u>Employer List</u></p> 
    </br> 


    <table width="1345" height="113" border="1" id="table1"> 
     <tr id="tr1"> 
     <th width="46" height="35" scope="col">No.</th> 
     <th width="93" scope="col">Title</th> 
     <th width="157" scope="col">First Name</th> 
     <th width="171" scope="col">Last Name</th> 
     <th width="128" scope="col">Position</th> 
     <th width="130" scope="col">Sector</th> 
     <th width="178" scope="col">Company Name</th> 
     <th width="107" scope="col">Country</th> 
     <th width="97" scope="col">Email</th> 
     <th width="78" scope="col">Phone</th> 
     <th width="84" scope="col">Action</th> 
     </tr> 
     <tr> 
     <td height="34">&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     </tr> 
     <tr> 
     <td height="34">&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
     </tr> 
    </table> 
    <br/> 
    <input name="addbutton" type="button" value="Add Employer" id="butt" onclick="showHide()"/> 
    <br/> 
    <br/> 
    <br/> 
    <form id="form1" action="add_employer.php" method="post" class="hidden"> 
    <p align="left"><u><strong>Add Employer Detail</strong></u></p> 
    <br/> 
    <br/> 
    <TABLE cellpadding="5" cellspacing="2" align="center"> 
        <TR> 
         <td><strong>Title </strong></td> 
         <td><strong>: 
         <input name="title" value="" type="text" size="50" maxlength="50"> 
         </strong></td> 
        </TR> 
        <TR> 
         <td><strong>First Name </strong></td> 
         <td><strong>: 
         <input name="first_name" value="" type="text" size="50" maxlength="50"> 
         </strong></td> 
        </TR> 
        <TR> 
         <td><strong>Last Name</strong></td> 
         <TD><strong>: 
         <input name="last_name" value="" type="text" size="50" maxlength="50"> 
         </strong></TD> 
        </TR> 
        <TR> 
         <td><strong>Position</strong></td> 
         <TD><strong>: 
         <input name="position" value="" type="text" size="50" maxlength="50"> 
         </strong></TD> 
        </TR> 
        <TR> 
         <td><strong>Sector</strong></td> 
         <TD><strong>: 
         <input name="sector" value="" type="text" size="50" maxlength="50"> 
         </strong></TD> 
        </TR> 
        <TR> 
         <td><strong>Company Name</strong></td> 
         <TD><strong>: 
         <input name="company_name" value="" type="text" size="50" maxlength="50"> 
         </strong></TD> 
        </TR> 
        <TR> 
         <td><strong>Country</strong></td> 
         <TD><strong>: 
         <input name="country" value="" type="text" size="50" maxlength="50"> 
         </strong></TD> 
        </TR> 
        <TR> 
         <td><strong>Email</strong></td> 
         <TD><strong>: 
         <input name="email" value="" type="text" size="50" maxlength="50"> 
         </strong></TD> 
        </TR> 
        <TR> 
         <td><strong>Phone</strong></td> 
         <TD><strong>: 
         <input name="phone" value="" type="text" size="50" maxlength="50"> 
         </strong></TD> 
        </TR> 
        <TR> 
         <td><strong>Action</strong></td> 
         <TD><strong>: 
         <input name="action" value="" type="text" size="50" maxlength="50"> 
         </strong> 
         <input name="addbutton" type="button" value="Add Employer"/> 
         <input name="clear" type="button" value="Clear"/> 
         </TD> 

        </TR>    

       </TABLE> 
    <br/> 
    <br/>  
    </form> 



    </body> 
    </html> 
+0

你婉到達這裏切換效果,即先點擊表格上會得到顯示和第二次點擊它會得到隱藏的等等? – Ruhul

+0

'getElementByClassName'不是一個函數。 'getElementsByClassName'是並返回一個節點列表。使用'[0]'訪問第一個元素。你也可以使用可見性CSS屬性 – Li357

回答

-1

你可以使用jQuery。

隱藏在按鈕上的onClick事件中執行此操作。

$(".target").hide(); 

有關顯示

$(".target").show(); 
+1

但是當OP使用vanilla JS並且沒有提及它時,jQuery有什麼意義? – Li357

+0

如果您檢查問題中顯示的代碼,OP已經有代碼來處理實際的隱藏和顯示,但似乎在適當的時候運行該代碼時遇到問題。 – nnnnnn

1

既然你隱藏的元素帶班,可以顯示並通過刪除並重新添加類隱藏它。該classList.toggle方法*將這樣做:

function hide(id) { 
 
    var el = document.getElementById(id); 
 
    if (el && el.classList) { 
 
    el.classList.toggle('hidden'); 
 
    } 
 
    
 
}
<style type="text/css"> 
 
    .hidden { 
 
    display: none; 
 
    } 
 
</style> 
 

 
<body> 
 
    
 
<input id="foo"><br> 
 
<button onclick="hide('foo')">Hide input</button> 
 

 
    
 
    
 
    
 
    
 
</body>

*注意參考MDN包括Element.classList一個填充工具以支持舊版瀏覽器。

+0

我不認爲將'.style.display'設置爲空字符串將顯示該元素,因爲它當前通過類隱藏。 – nnnnnn

+0

@ nnnnnn-yep,oops ... – RobG

+0

P.S.關於三元運算符可能是一個維護問題的說明,這是一個意見問題,但我個人認爲它使得代碼更清晰:它可以讓你讀代碼爲「將該屬性設置爲[某些計算值]「,相比之下,」評估該財產和做x其他y「,然後它需要額外的時間來認識到,實際上x和y只是設置相同的屬性。它也恰好變短的事實僅僅是一種獎勵。 – nnnnnn

1

您可以使用id來定位form並添加刪除class以顯示隱藏表格。

<style> 
     .hidden { 
      display:none;} 

    </style> 

    <script type="text/javascript"> 
     function showHide() { 
      var hiddeninput = document.getElementById("form1"); 
      if (hiddeninput.classList.contains("hidden")) { 
       hiddeninput.classList.remove("hidden"); 
      } 
      else { 
       hiddeninput.classList.add("hidden"); 
      } 
     } 

    </script> 
0

這個怎麼樣???

function showHide() { 
 
    var button = document.getElementById("butt"); 
 
    var hiddeninput = document.getElementsByClassName("hidden"); 
 
    var form1 = document.getElementById("form1"); 
 
    if (form1 && form1.style.display=="none") { 
 
    form1.style.display = "block";  
 
    } else { 
 
    form1.style.display = "none"; 
 
    } 
 
}
<div id="navbar"> 
 
    <div id="holder"> 
 

 
    <ul> 
 
     <li><a href="employee.php" id="onlink">Employer Database</a> 
 
     </li> 
 
     <li><a href="companypresence.php">Company Presence</a> 
 
     </li> 
 
     <li><a href="companypromotion.php">Company Promotion</a> 
 
     </li> 
 
    </ul> 
 
    </div> 
 
    <!--end of navbar div --> 
 

 
    </br> 
 

 
    <p id="p1"><u>Employer List</u> 
 
    </p> 
 
    </br> 
 

 

 
    <table width="1345" height="113" border="1" id="table1"> 
 
    <tr id="tr1"> 
 
     <th width="46" height="35" scope="col">No.</th> 
 
     <th width="93" scope="col">Title</th> 
 
     <th width="157" scope="col">First Name</th> 
 
     <th width="171" scope="col">Last Name</th> 
 
     <th width="128" scope="col">Position</th> 
 
     <th width="130" scope="col">Sector</th> 
 
     <th width="178" scope="col">Company Name</th> 
 
     <th width="107" scope="col">Country</th> 
 
     <th width="97" scope="col">Email</th> 
 
     <th width="78" scope="col">Phone</th> 
 
     <th width="84" scope="col">Action</th> 
 
    </tr> 
 
    <tr> 
 
     <td height="34">&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td height="34">&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    </table> 
 
    <br/> 
 
    <input name="addbutton" type="button" value="Add Employer" id="butt" onclick="showHide()" /> 
 
    <br/> 
 
    <br/> 
 
    <br/> 
 

 
    <form id="form1" action="add_employer.php" method="post" style="display: none;"> 
 
    <p align="left"><u><strong>Add Employer Detail</strong></u> 
 
    </p> 
 
    <br/> 
 
    <br/> 
 
    <TABLE cellpadding="5" cellspacing="2" align="center"> 
 
     <TR> 
 
     <td><strong>Title </strong> 
 
     </td> 
 
     <td><strong>: 
 
         <input name="title" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
     </td> 
 
     </TR> 
 
     <TR> 
 
     <td><strong>First Name </strong> 
 
     </td> 
 
     <td><strong>: 
 
         <input name="first_name" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
     </td> 
 
     </TR> 
 
     <TR> 
 
     <td><strong>Last Name</strong> 
 
     </td> 
 
     <TD><strong>: 
 
         <input name="last_name" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
     </TD> 
 
     </TR> 
 
     <TR> 
 
     <td><strong>Position</strong> 
 
     </td> 
 
     <TD><strong>: 
 
         <input name="position" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
     </TD> 
 
     </TR> 
 
     <TR> 
 
     <td><strong>Sector</strong> 
 
     </td> 
 
     <TD><strong>: 
 
         <input name="sector" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
     </TD> 
 
     </TR> 
 
     <TR> 
 
     <td><strong>Company Name</strong> 
 
     </td> 
 
     <TD><strong>: 
 
         <input name="company_name" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
     </TD> 
 
     </TR> 
 
     <TR> 
 
     <td><strong>Country</strong> 
 
     </td> 
 
     <TD><strong>: 
 
         <input name="country" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
     </TD> 
 
     </TR> 
 
     <TR> 
 
     <td><strong>Email</strong> 
 
     </td> 
 
     <TD><strong>: 
 
         <input name="email" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
     </TD> 
 
     </TR> 
 
     <TR> 
 
     <td><strong>Phone</strong> 
 
     </td> 
 
     <TD><strong>: 
 
         <input name="phone" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
     </TD> 
 
     </TR> 
 
     <TR> 
 
     <td><strong>Action</strong> 
 
     </td> 
 
     <TD><strong>: 
 
         <input name="action" value="" type="text" size="50" maxlength="50"> 
 
         </strong> 
 
      <input name="addbutton" type="button" value="Add Employer" /> 
 
      <input name="clear" type="button" value="Clear" /> 
 
     </TD> 
 

 
     </TR> 
 

 
    </TABLE> 
 
    <br/> 
 
    <br/> 
 
    </form>

+1

這與OP的不同之處在於,該元素用類隱藏,而不是顯示屬性。雖然它有效,但應該解釋不同的方法,並討論與OP相關的利弊。 – RobG

相關問題