开发者

Hide/Show form in html?

I'm new to html and im trying to hide/show forms if the user ticks a box a form appears below it.

I have a form e开发者_开发知识库.g.

Name : 
Username : 
Add More Users: (tickbox) 

If the user ticks the 'Add More Users', another form with the fields 'Name,Username,Add More Users' appears below it. How is this possible?


HTML

<input type="checkbox" onclick="display(this)">Add more users</input>
<div id="yourDiv"> ...other forms... </div>

JavaScript

   function display(e){
    if (e.checked)
        document.getElementById('yourDiv').style.display = 'block';
    else
        document.getElementById('yourDiv').style.display = 'none';
    }

An even better way to do this (thanks to What)

HTML

<input id="more" type="checkbox">Add More Users</input>
<div id="yourDiv"> ...other form... </div>

JavaScript

window.onload = function () {
    document.getElementById('more').onclick = function () {
        if (this.checked)
            document.getElementById('yourDiv').style.display = 'block';
        else
            document.getElementById('yourDiv').style.display = 'none';  
    }
}


You will need to use scripts for that.

First of all put the form you want to Show/Hide inside a <div id="myForm"> tag and enclose it with

Then use jQuery http://www.jquery.com (download the jquery library and link it to your page and add an event at the loading of the page to run a function to check if the combo box is checked then execute:

("#myForm").toggle();


You can use .clone() and .append() (requires jQuery). You have give name attribute like

<input type="text" name="test[]" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜