现在的位置: 首页 > 综合 > 正文

Asp.net:DropDownList的輸入與選取3/16

2014年09月05日 ⁄ 综合 ⁄ 共 2244字 ⁄ 字号 评论关闭

在Asp.net中經常要用到DropDownList控件,比如可以選取客戶代碼,客戶名稱或者會計科目,科目名稱,而當USER的資料比較多時,不可以直接在上面輸入,而逐筆點選對USER來講著實讓人頭疼,如下:

Value                   Text

111101               現金-RMB

111102               現金-HKD

...

6141001            銷售費用-薪資支出

6141002           銷售費用-差旅費

6141003           銷售費用-郵費

...

6241001           管理費用-薪資支出

6241002           管理費用-差旅費

6241003           管理費用-郵費

...

USER希望能錄入6141時就跑到6141001這裏或者錄入6141001時就直接選取了

被這個問題困擾了好久,在一位網友的幫助下,今天總算找到了一個還算滿意的解決方法^__^,方法如下:(輸入value,秀出相關的text)

function catch_keydown(sel) { switch(event.keyCode) { case 13: //Enter; //sel.options[sel.length] = new Option("","",false,true); //event.returnValue = false; //break; case 27: //Esc; //alert("text:" + sel.options[sel.selectedIndex].text + ", value:" + sel.options[sel.selectedIndex].value + ";"); //event.returnValue = false; break; case 46: //Delete; //if(confirm("刪除當前內容!?")) { //sel.options[sel.selectedIndex] = null; //if(sel.length>0) { sel.options[0].selected = true; } } //event.returnValue = false; break; case 8: //Back Space; //var s = sel.options[sel.selectedIndex].text; var s = sel.options[0].text; sel.options[0].text = s.substr(0,s.length-1); event.returnValue = false; break; } } //僅在第一個選項上顯示輸入的值

function catch_press(sel) { if (sel.selectedIndex==0){ sel.options[sel.selectedIndex].text = sel.options[sel.selectedIndex].text + String.fromCharCode (event.keyCode); event.returnValue = false;} } //僅在第一個選項上輸入值

function ca() { var select_obj = document.getElementById("DropDownList1"); var aa=select_obj.options[0].text; var qq=aa.length; if (select_obj.options.length) { for (var i=0;i< select_obj.options.length ;i++) { if (select_obj.options[i].value.substr(0,qq)==aa) { //alert("存在"); select_obj.selectedIndex=i; return;} } } }//焦點離開後秀出值的名稱

<asp:dropdownlist onkeypress="catch_press(this);" id="DropDownList1" onkeydown="catch_keydown(this);"
onblur="ca()" style="Z-INDEX: 101; LEFT: 336px; POSITION: absolute; TOP: 144px" tabIndex="1" runat="server"
Width="160px">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="5141001">002</asp:ListItem>
<asp:ListItem Value="6141001">aaaa</asp:ListItem>
<asp:ListItem Value="6141002">aaab</asp:ListItem>
<asp:ListItem Value="6141003">aaac</asp:ListItem>
<asp:ListItem Value="6141004">aaad</asp:ListItem>
<asp:ListItem Value="6241001">aaae</asp:ListItem>
<asp:ListItem Value="6241002">aaag</asp:ListItem>
</asp:dropdownlist>

另外,在一個事件中調用另一個事件的JS寫法如:onblur()="ca();this.onchange();"

抱歉!评论已关闭.