I have a form like this:
<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>
And want to select, for example, all those options containing the text 'nose' using a span tag onclick event around some text.
Can you help me with the onclick javascript code?
Thanks.
You have to iterate over the <option>
elements, check the contents and set the selected
property accordingly:
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);
}
You could do it like this:
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.