Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

html - Use javascript to close all opened forms before opening a new one

I'm making an html page with images, where the user clicks their image, to log in to their invoice page. This is what I have so far:

<script type="text/javascript">
   function showHideForm(formid) 
    {
       var _form = document.getElementById(formid);
      _form.style.display = (_form.style.display != "block") ? "block" : "none";
       return false;
    }
</script>

<p>
  <a href="#" onclick="return showHideForm('hiddenForm1');">
    <img src="" width="150" height=    "150" />
  </a>
</p>
<form  id="hiddenForm1" style="display: none;" method="post" action="user1_login.php">
  <input type="text" name="user" placeholder="Name" required /><br />
  <input type="text" name="pass" placeholder="Password" required/><br />
  <input type="submit" name="Submit" value="Submit" />
</form>

<p>
  <a href="#" onclick="return showHideForm('hiddenForm2');">
    <img src=""  width="150" height="150" />
  </a>
</p>
<form  id="hiddenForm2" style="display: none;" method="post" action="user2_login.php">
  <input type="text" name="user" placeholder="Name" required /><br />
  <input type="text" name="pass" placeholder="Password" required/><br />
  <input type="submit" name="Submit" value="Submit" />
</form>

It works nicely except that if you click on other images, you get several instances open at the same time.

Is it possible to tack a bit of code to the beginning of the javascript to close any open instances before it runs the code to open a new form?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The logic is simple, have a class for openedform. On click event remove that class from the existing opened forms and add the class to the currently clicked form. Here is how to do it with jquery.

 function showHideForm(formid) 
 {
    // var _form=$("#"+formid);  (in jquery)

    var _form =document.getElementById(formid);
    //select all existing opened forms and remove the class
    $('.openedForm').removeClass('openedForm');
    //add the openedForm class to the currently clicked 
    $(_form).addClass('openedForm');
    return false;
 }

Add the following code into your css file.

  .openedForm
     {
        display:block !important;
     }

If you need to alter the css of elements using javascript try to use classes. This way you make your javascript code cleaner and readable and your styling logic stays separated from the main logic. Avoid using inline styles as much as possible.

Working demo:
https://jsbin.com/nigivu/edit?html,css,js,output


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...