2012年6月27日水曜日

2012年6月11日月曜日

selectedIndex を取得する

function getSelectedIndexByValue(selectEl, value)
{
 var options = selectEl.options;
 var result = 0;
 for (var i = 0; i < options.length; i++)
 {
  var option = options[i];
  if (option.value == value)
  {
   result = i;
   berak;
  }
 }
 return result;
}
表示させている文字をキーとする場合は、8行目を option.text に変更する。