简体   繁体   English

javascript,使用从值输入文本字段的数组动态填充下拉列表

[英]javascript to dynamically populate dropdown with an array from value typed into text field

Hi I have a code example available here: http://jsbin.com/oxoweh 嗨,我这里有一个代码示例: http : //jsbin.com/oxoweh

My problem is that I cannot get the value that is being entered into the textbox so that when the select button is clicked the second dropdown will display only the data related to what was entered into the text box. 我的问题是我无法获取在文本框中输入的值,因此,当单击选择按钮时,第二个下拉列表将仅显示与在文本框中输入的内容有关的数据。 for example if fruits is entered into the textbox I want to have the subcat dropdown have all fruits in it instead of using the existing category dropdown menu 例如,如果在文本框中输入了水果,我想让subcat下拉菜单中包含所有水果,而不是使用现有的类别下拉菜单

http://jsfiddle.net/goldentoa11/P3hs8/1/ http://jsfiddle.net/goldentoa11/P3hs8/1/

I added a parameter to your "SelectSubCat()" called txtValue. 我向您的“ SelectSubCat()”添加了一个名为txtValue的参数。 This is so you can pass the value you want to compare against. 这样,您就可以传递要与之比较的值。

You'll need to call the "SelectSubCat()" function with the text field's value when you call it. 调用时,需要使用文本字段的值来调用“ SelectSubCat()”函数。 I added an id to the text field called "selectText", and when I click the button I had added 我在名为“ selectText”的文本字段中添加了一个ID,并在单击按钮时添加了

onclick="SelectSubCat(document.getElementById('selectText').value)"

and this will call the function with the text of the input. 这将使用输入文本来调用该函数。 Then instead of using the value from the form, it uses the value passed to it, and creates the appropriate sub category. 然后,它不使用表单中的值,而是使用传递给它的值,并创建适当的子类别。 Lastly, you need to change the Category's "onchange" function to just pass an empty string. 最后,您需要更改类别的“ onchange”功能以仅传递一个空字符串。

function SelectSubCat(txtValue) {

if(txtValue == "") txtValue = document.drop_list.Category.value;
// This line is to set txtValue to Category's value if the current value is blank. 
// In other words, if it was called by changing the dropdown, 
// give it the drop down's value.

// ON selection of category this function will work
removeAllOptions(document.drop_list.SubCat);
addOption(document.drop_list.SubCat, "", "SubCat", "");

if (txtValue == 'Fruits') {
    addOption(document.drop_list.SubCat, "Mango", "Mango");
    addOption(document.drop_list.SubCat, "Banana", "Banana");
    addOption(document.drop_list.SubCat, "Orange", "Orange");
}
if (txtValue == 'Games') {
    addOption(document.drop_list.SubCat, "Cricket", "Cricket");
    addOption(document.drop_list.SubCat, "Football", "Football");
    addOption(document.drop_list.SubCat, "Polo", "Polo", "");
}
if (txtValue == 'Scripts') {
    addOption(document.drop_list.SubCat, "PHP", "PHP");
    addOption(document.drop_list.SubCat, "ASP", "ASP");
    addOption(document.drop_list.SubCat, "Perl", "Perl");
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript从PHP数组中填充带有选项的动态创建的下拉列表 - Populate Dynamically Created Dropdown with Options from PHP array using javascript 如何从文本框中动态填充下拉列表 - How to dynamically populate a dropdown from a text box 使用 ACF 字段中的内容动态填充联系表 7 下拉字段 - Dynamically populate Contact Form 7 dropdown field with content from ACF field 根据下拉选择动态填充字段(Javascript / HTML) - Dynamically populate a field based on dropdown selection (Javascript / HTML) 使用“文本”和“值”填充XML的下拉列表 - Populate Dropdown from XML with“text” & “value” 使用数组从下拉列表获取文本字段值时遇到问题 - Trouble getting text field value from a dropdown, using an array 根据下拉字段选择从数据库填充表单文本字段 - Populate form text field from database based on dropdown field selection 如何根据下拉列表中的选择填充文本字段的默认值或将其保留为空? - How to populate a text field with its default value or leave it empty, all according to a choice from a dropdown list? 动态填充JavaScript数组 - Populate a JavaScript array dynamically 从变量值动态填充JSON数组 - Dynamically populate JSON array from variable value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM