<select id="s" onchange="selectchange(this);">
<option value="1">文本框</option>
<option value="2">复选框</option>
<option value="3">单选框</option>
</select>
<div id="changeDiv"></div>
<script type="text/javascript">
function selectchange(id)
{
var changeDiv = document.getElementById("changeDiv");
if(id.options[id.selectedIndex].value == 1)
{
if(changeDiv.hasChildNodes())
{
changeDiv.removeChild(changeDiv.childNodes[0]);
var text = document.createElement("input");
text.setAttribute("id","text1");
text.setAttribute("type","text");
text.setAttribute("value","文本框");
changeDiv.appendChild(text);
}
else
{
var text = document.createElement("input");
text.setAttribute("id","text1");
text.setAttribute("type","text");
text.setAttribute("value","文本框");
changeDiv.appendChild(text);
}
}
else if(id.options[id.selectedIndex].value == 2)
{
if(changeDiv.hasChildNodes())
{
changeDiv.removeChild(changeDiv.childNodes[0]);
var checkbox = document.createElement("input");
checkbox.setAttribute("id","check");
checkbox.setAttribute("type","checkbox");
checkbox.setAttribute("value","复选框");
changeDiv.appendChild(checkbox);
}
else
{
var checkbox = document.createElement("input");
checkbox.setAttribute("id","check");
checkbox.setAttribute("type","checkbox");
checkbox.setAttribute("value","复选框");
changeDiv.appendChild(checkbox);
}
}
else if(id.options[id.selectedIndex].value == 3)
{
if(changeDiv.hasChildNodes())
{
changeDiv.removeChild(changeDiv.childNodes[0]);
var radio = document.createElement("input");
radio.setAttribute("name","radio1");
radio.setAttribute("type","radio");
radio.setAttribute("value","单选框");
changeDiv.appendChild(radio);
}
else
{
var radio = document.createElement("input");
radio.setAttribute("name","radio1");
radio.setAttribute("type","radio");
radio.setAttribute("value","单选框");
changeDiv.appendChild(radio);
}
}
}
</script>
温馨提示:答案为网友推荐,仅供参考