I have the following jQuery method that iterates over a selectlist containing a number of options. I want to hihlight the first line that has a match for some text typed by the user into a textbox named MMDCriteria. Currently the code is selecting and highlighting each item in turn until it gets to the end of the list.
$("#MMDCriteria").bind('keypress', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
var criteria = $(this).val().toUpperCase();
alert("");
$('select#MMD > option').each(function() {
var optionValue = $(this).text().toUpperCase();
alert(optionValue.substring(0, optionValue.indexOf(criteria)));
if(optionValue.substring(0, optionValue.indexOf(criteria)) > -1) {
$(this).attr('selected', 'selected');
}
});
}
});
I have only just started using jQuery so excuse the verboseness.
以上就是Iterating a selectlist to highlight an item, highlights each item in turn的详细内容,更多请关注web前端其它相关文章!