选择选项时添加输入文本

问题描述:

我如何添加一个时之间的特定选项标签被选中,然后,如果选择了另一种选择,使用jQuery删除文本字段?

How can I add an an <input type="text"> when a specific option between <select></select> tags is selected, then, if another option is selected, delete that text field using jQuery?

您应使用change事件.然后,您可以比较要为其创建输入的值,并将其添加到body或任何其他元素中.

You should make use of change event. Then you could compare the value for which you want to create the input and add it to the body or to any other element.

生活示例: http://jsfiddle.net/MKPdb/

具有这样的列表,其ID为mylist,例如:

Having a list like this, with the id mylist, for example:

<select id="mylist">
 ...
</select>

您可以使用:

$('#mylist').change(function(){
    if( $(this).val() == '1'){
        $('body').append('<input id="myInput" type="text" />');
    }else{
        $('#myInput').remove();
    }
});

生活示例: http://jsfiddle.net/MKPdb/