使用文本框中的jquery填充下拉列表

我有一个文本框,其id为other,我想将此文本框值填充到下拉列表中这是我用来填充的javascript …但这不起作用..任何建议都将被赞赏… !! 我想这样做: – http://viralpatel.net/blogs/demo/dynamic-combobox-listbox-dropdown-in-javascript.html

 function addCombo() { var textb = $('#other').attr('id'); var combo = $('#category').attr('id'); alert(combo); var option = document.createElement("option"); option.text = $('#other').val(); alert(option.text); option.value = $('#other').val(); try { combo.add(option, null); //Standard }catch(error) { combo.add(option); // IE only } textb.value = ""; }  

这是下拉列表的代码

 Category  Select Food Rent Gas Entertainment Grocery General Other   Other    

我相信它是因为

 var textb = $('#other').attr('id'); var combo = $('#category').attr('id'); 

你不是将这些元素的id分配给变量而不是元素吗?
我的意思是…这个代码执行后textb真的是一个输入元素,还是只是一个字符串“other”?

试试这个:

 var textb = $('#other'); var combo = $('#category'); 

编辑:

 function addCombo() { var textb = document.getElementById("other"); var combo = document.getElementById("category"); var option = document.createElement("option"); option.text = textb.value; option.value = textb.value; try { combo.add(option, null); //Standard }catch(error) { combo.add(option); // IE only } textb.value = ""; } 

既然你已经在使用jQuery了:

 function addCombo() { var optName = $("#other").attr("value"); $('#category').append(""); $("#other").attr("value", ""); }