使用文本框内的jQuery填充下拉列表 [英] Populate Drop Down List using jquery from textbox
问题描述
我有一个ID为其他的文本框,我想将此文本框值填充到下拉列表中 这是我用来填充的javascript...但是这是行不通的.任何建议将不胜感激……!我想这样做: http://viralpatel.net/blogs/demo/dynamic -combobox-listbox-dropdown-in-javascript.html
I have a textbox whose id is other and I want to populate this textbox value into drop down list This is javascript I am using to populate... but this is not working.. Any suggestion will be appreciated...!! I want to do like this:- http://viralpatel.net/blogs/demo/dynamic-combobox-listbox-dropdown-in-javascript.html
<SCRIPT language="javascript">
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 = "";
}
</SCRIPT>
这是下拉列表的代码
This is the code of drop down list
<td>Category</td>
<td><select class="size" id="category" name="category" width="30px">
<option width="30px" value="" selected="selected" >Select</option>
<option value="food">Food</option>
<option value="rent">Rent</option>
<option value="gas">Gas</option>
<option value="enter">Entertainment</option>
<option value="grocery">Grocery</option>
<option value="general">General</option>
<option value="other">Other</option></select></td>
</tr>
<tr>
<td>Other</td>
<td><input type="text" id="other" name="other"/></td>
<td><input type="button" data-role="button" value="Add" onclick="addCombo()"></td>
</tr>
推荐答案
我相信是因为
var textb = $('#other').attr('id');
var combo = $('#category').attr('id');
不是将这些元素的ID分配给变量而不是元素吗?
我的意思是……执行此代码后,textb真的是输入元素,还是只是字符串"other"?
Aren't you assigning id's of these elements to variables, instead of elements?
I mean... is textb really a input element after this code executes, or is it just a string "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填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!