document.getElementById(“test”).style.display=“hidden” not working

后端 未结 11 963
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 14:10

I want to hide my form when I click on the submit button. My code is as follows:



        
相关标签:
11条回答
  • 2021-02-02 14:41

    you can use something like this....div container

    <script type="text/javascript">
    function hide(){
    document.getElementById("test").innerHTML.style.display="none";
    }
    </script>
    <div id="test">
    <form method="post" >
    
    <table width="60%" border="0" cellspacing="2" cellpadding="2"  >
      <tr style="background:url(../images/nav.png) repeat-x; color:#fff; font-weight:bold" align="center">
        <td>Ample Id</td>
        <td>Find</td>
      </tr>
    
      <tr align="center" bgcolor="#E8F8FF" style="color:#006" >
        <td><input type="text" name="ampid" id="ampid" value="<?php echo $_POST['ampid'];?>" /></td>
       <td><input type="image" src="../images/btnFind.png" id="find" name="find" onclick="javascript:hide();"/></td>
       </tr>
    
    </table>
    
    </form>
    </div>
    
    0 讨论(0)
  • 2021-02-02 14:43

    It should be either

    document.getElementById("test").style.display = "none";
    

    or

    document.getElementById("test").style.visibility = "hidden";
    

    Second option will display some blank space where the form was initially present , where as the first option doesn't

    0 讨论(0)
  • 2021-02-02 14:45

    its a block element, and you need to use none

    document.getElementById("test").style.display="none"
    

    hidden is used for visibility

    0 讨论(0)
  • 2021-02-02 14:47

    Set CSS display property to none.

    document.getElementById("test").style.display = "none";
    

    Also, you do not need javascript: for the onclick attribute.

    <input type="image" src="../images/btnFind.png" id="find" name="find" 
        onclick="hide();" />
    

    Finally, make sure you do not have multiple elements with the same ID.

    If your form goes nowhere, Phil suggested that you should prevent submission of the form. Simply return false in the onsubmit handler.

    <form method="post" id="test" onsubmit="return false;">
    

    If you want the form to post, but hide the div on subsequent page load, you will have to use server-side code to hide the element:

    <script type="text/javascript">
    function hide() {
        document.getElementById("test").style.display = "none";
    }
    window.onload = function() {
        // if form was submitted, PHP will print the below, 
        //    which runs function hide() on page load
        <?= ($_POST['ampid'] != '') ? 'hide();' : '' ?>
    }
    </script>
    
    0 讨论(0)
  • 2021-02-02 14:47

    Replace hidden with none. See MDN reference.

    0 讨论(0)
  • 2021-02-02 14:47

    you need to use display = none

    value hidden is connected with attributet called visibility

    so your code should look like this

    <script type="text/javascript">
    function hide(){
    document.getElementById("test").style.display="none";
    }
    </script>
    
    0 讨论(0)
提交回复
热议问题