假设我在某个变量中有'v'值'var',现在我想显示Valvo(文本尊重值v)。 任何建议??
答案 0 :(得分:0)
HTML
<select id="sel">
<option value="Volvo">Volvo</option>
<option value="BMW">BMW</option>
</select>
JS
var myvar = 'b';
var elem = document.querySelector('#sel option[value^='+myvar.toUpperCase()+']');
if (typeof elem != 'undefined') {
elem.selected = true;
}
在这里你可以测试 - &gt; http://jsbin.com/ehayav/1
MZ
答案 1 :(得分:0)
这可能会对你有所帮助:
//array of varables
var names = ['apple','banana','valvo','blahblahblah']
//this is your variable
var variable = 'v'
//code to display names starting with the character in your variable
for(i=0;i<names.length;i++)
if(names[i][0] == variable)
alert(names[i])
这个答案是我对你的问题的解释。享受!