你可以这样说:
$("select#type").change(function(){
var value = $(this).val();
$("div#target").append('<input type="textbox" value='+value+'/>');
});
在此的jsfiddle看看的工作例如:http://jsfiddle.net/v7yHU/
更新更加优雅和逻辑分离你的HTML。 HTML:
<select name="type" id="type" >
<option >Select a value...</option>
<option value="fooa">Add 2 Texboxes</option>
<option value="foob">Add 3 radios</option>
<option value="fooc">add 2 checkboxes and 2 textboxes</option>
</select>
<div id="target">
<div id="fooa">
<input type="textbox" value="tb1"/>
<input type="textbox" value="tb2"/>
</div>
<div id="foob">
<input type="radio" value="rb1"/>
<input type="radio" value="rb2"/>
<input type="radio" value="rb3"/>
</div>
<div id="fooc">
<input type="textbox" value="tb1"/>
<input type="textbox" value="tb2"/>
<input type="checkbox" value="cb1"/>
<input type="checkbox" value="cb2"/>
</div>
</div>
的JavaScript:
$(document).ready(function(){
//hide all in target div
$("div", "div#target").hide();
$("select#type").change(function(){
// hide previously shown in target div
$("div", "div#target").hide();
// read id from your select
var value = $(this).val();
// show rlrment with selected id
$("div#"+value).show();
});
});
工作样本:http://jsfiddle.net/v7yHU/3/
有了这个js你可以有n个选择选项,无需更改脚本。
原来这是更好的解决方案。我需要2个下拉框和上面的冲突。您的更新非常流畅,谢谢! – john