我有一个 javascript 函数,它应该用传递给函数的字母开头的数组中的所有项目填充一个选择框。我唯一的问题是我无法让我的正则表达式语句/编码工作。这是我的功能:
function replaceCompanySelect (letter)
{
var list = document.getElementById("company"); //Declare the select box as a variable
list.options.length=0; //Delete the existing options
list.options[0]=new Option("Please Select a Company", "0", false, false); //Add the first option in
for(var i=1;i<companies.length;i++) //For each company in the array
{
if(companies[i].match("/\b"+letter+"/g") != null && (letter != 'undefined' ||letter != 'undefined')) //If the company starts with the correct letter and the position's value is not undefined or empty
{
alert(companies[i]); //Only used for testing purposes, code should be as above loop to I used to insert the 1st option
}
}
}
有任何想法吗?