这是一个避免使用 jQuery 的解决方案,它会根据需要隐藏您的表单。工作 jsFiddle 供您参考: http: //jsfiddle.net/vwnP6/1/:
表单的 HTML:
<form>
<input type="radio" name="postTo" id = "facebook" value="Facebook" checked="checked"/>Facebook
<input type="radio" name="postTo" id = "twitter" value="Twitter"/>Twitter
<input type="radio" name="postTo" id = "tumblr" value="Tumblr"/>Tumblr
<input type="radio" name="postTo" id = "instagram" value="Instagram"/>Instagram
</form>
<form id="facebookForm" action="" method="">
<h2>Facebook</h2>
<p><label for="postTitle">Title:</label> <input type="text" id="postTitle" /></p>
<p><label for="postContent">Content:</label> <input type="text" id="postContent" /></p>
</form>
<form id="twitterForm" action="" method="">
<h2>Twitter</h2>
<p><label for="postTitle">Title:</label> <input type="text" id="postTitle" /></p>
<p><label for="postContent">Content:</label> <input type="text" id="postContent" /></p>
</form>
<form id="tumblrForm" action="" method="">
<h2>Tumblr</h2>
<p><label for="postTitle">Title:</label> <input type="text" id="postTitle" /></p>
<p><label for="postContent">Content:</label> <input type="text" id="postContent" /></p>
</form>
<form id="instagramForm" action="" method="">
<h2>Instagram</h2>
<p><label for="postTitle">Title:</label> <input type="text" id="postTitle" /></p>
<p><label for="postContent">Content:</label> <input type="text" id="postContent" /></p>
</form>
Javascript:
// Our various radio buttons
// We can define these globally since that's how Javascript works
// Would be better to have this all encapsulated into its own "class" to keep namespace clean
var facebook_radio = document.getElementById('facebook');
var twitter_radio = document.getElementById('twitter');
var tumblr_radio = document.getElementById('tumblr');
var instagram_radio = document.getElementById('instagram');
function hideElement(element) {
element.style.display = "none";
element.style.visibility = "hidden";
}
function showElement(element) {
element.style.display = "block";
element.style.visibility = "visible";
}
function changeForm() {
/* Hide all forms and show desired form based on checked radio button */
// Our form elements
var facebook_form = document.getElementById("facebookForm");
var twitter_form = document.getElementById("twitterForm");
var tumblr_form = document.getElementById("tumblrForm");
var instagram_form = document.getElementById("instagramForm");
// Hide all of our forms
hideElement(facebook_form);
hideElement(twitter_form);
hideElement(tumblr_form);
hideElement(instagram_form);
// Show appropriate form
if (facebook_radio.checked)
{
showElement(facebook_form);
}
else if (document.getElementById('twitter').checked)
{
showElement(twitter_form);
}
else if (document.getElementById('tumblr').checked)
{
showElement(tumblr_form);
}
else if (document.getElementById('instagram').checked)
{
showElement(instagram_form);
}
}
// When you select a radio button, you want to change to correct form
facebook_radio.onchange = changeForm;
twitter_radio.onchange = changeForm;
tumblr_radio.onchange = changeForm;
instagram_radio.onchange = changeForm;
// Show first checked form
changeForm();