背景 - 我有一個帶有文本字段的表單。用戶輸入2-6個數字的整數。在每個關鍵筆畫上,javascript函數被稱爲匹配下拉列表中的值。onKeyUp函數不返回所有結果
我的文本字段:
<input type="text" name="orgSICCode" value="" allownull="FALSE" size="5" maxlength="10" datatype="dtNumeric" onKeyUp="ActivateOption(description,this.value);" onBlur="this.value = description.options[description.selectedIndex].value;" emsg="Must choose a SIC Code before you can continue.">
我下拉字段:
<select name="description" STYLE="width:275px;" onChange="orgSICCode.value = this.options[this.selectedIndex].value;sic_code_description.value = this.options[this.selectedIndex].text;">
<option value="">Select One ...</option>
<option value="085101">085101 - Forest management services</option>
<option value="0831">0831 - Forest Products</option>
<option value="083100">083100 - Forest products</option>
<option value="083199">083199 - Forest products, nec</option>
<option value="08">08 - Forestry</option>
</select>
我的JavaScript功能ActivateOption:
function ActivateOption(selectObj, strValue) {
for(var idx=0;idx<selectObj.options.length;idx++) {
if(selectObj.options[idx].value.substring(0,strValue.length) == strValue) {
selectObj.selectedIndex = idx;
return true;
}
}
selectObj.selectedIndex = 0;
return false;
}
當用戶鍵入 '08',第一個選項應該填入08值,「08 - 林業」選項。填充的選項是'085101','085101 - 森林管理服務'。
我做錯了什麼?
謝謝。
感謝您的答覆亞當,但似乎並不奏效。它正在返回類似的結果到我目前的功能。 – etm124 2012-02-17 14:41:10
它似乎是我的條件的第一部分匹配我的'strValue'的任何值,而不是FIRST。 – etm124 2012-02-17 15:11:37