我有一个这样的表格:
<form action="/cgi-bin/cgi_info.py" method="POST">
<select id="faults" class="multiselect" multiple="multiple" name="faults[]">
<option>Big nose
<option>Big feet
<option>Wrinkly nose
<option>Wrinkly feet
<option>Spotty nose
<option>Spotty feet
</select>
</form>
并且想要选择,例如,包含文本'nose'的所有选项,使用span标记onclick事件围绕某些文本。
你能帮我解决一下onclick javascript代码吗?
感谢。
答案 0 :(得分:4)
您必须遍历<option>
元素,检查内容并相应地设置selected
属性:
var faults = document.getElementById("faults").options,
reg = /\bnose\b/;
for (var i=0, max = faults.length; i < max; i++) {
faults[i].selected = reg.test(faults[i].innerHTML);
}
答案 1 :(得分:0)
你可以这样做:
<form action="/cgi-bin/cgi_info.py" method="POST">
<select id="faults" class="multiselect" multiple="multiple" name="faults[]">
<option>Big nose
<option>Big feet
<option>Wrinkly nose
<option>Wrinkly feet
<option>Spotty nose
<option>Spotty feet
</select>
</form>
<script>
function search(){
for (i=0; i <= document.getElementById('faults').options.length - 1; i++){
if (document.getElementById('faults').options[i].text.indexOf('nose') > 0) {
alert(document.getElementById('faults').options[i].text);
}
}
}
</script>
<input type="button" value="Find by text" onclick="search();"></input>