2013-12-11 21 views
0

我在頁面加載時爲組織名稱列表動態構建SELECT表單對象。我使用的是數字值,因爲我根據選擇框引用了組織信息。將SELECT對象的選定值提交給動態創建的列表

我想動態改變一個隱藏字段的值時,SELECT框被改變,但我只能得到的價值。我嘗試在OPTION標籤中包含組織名稱作爲「標題」,但似乎無法檢索標題。任何建議將不勝感激。

代碼來構建選擇框:

dim OrgList(200) 
OrgSelect="<select name='Orgs' onchange='OrgPick(this);'>"&vbCrLf 
OrgSelect=OrgSelect&"<option value='none' selected>Select one...</option>"&vbCrLf 
OrgCount=0 
rOrgList.movefirst 
do while not rOrgList.eof 
    OrgList(OrgCount)=rOrgList("OrganizationName") 
    OrgSelect=OrgSelect&"<option value='"&OrgCount&"'>"&rOrgList("OrganizationName")&"</option>"&vbCrLf 
    OrgCount=OrgCount+1 
    rOrgList.movenext 
loop 
OrgSelect=OrgSelect&"</select>" 

更新 - 2013年12月11日11:22

我OrgPick()JavaScript函數,沒有工作:

function OrgPick(data) 
{ 
    document.getElementById("yourPick").value = data.title; 
} 

此外,將選擇框代碼的第8行更改爲:

OrgSelect=OrgSelect&"<option value='"&OrgCount&"' title='"&rOrgList("OrganizationName")&"'>"&rOrgList("OrganizationName")&"</option>"&vbCrLf 
+0

OrgPick()函數是做什麼的?你能爲此發佈代碼嗎? –

+0

「OrgPick()」函數正是我用來更新隱藏值對象的。 – dcoughler

回答

1

我已經重寫了一些ASP經典代碼,只是爲了使它更易於閱讀。

'Create the HTML for the organization <Select> list 
'and send it to the response stream 
response.write MakeOrgSelectList("Orgs", "getSelectedOptionText(this);", rOrgList) 

' Function MakeOrgSelectList() 
' Parameter name: The HTML name of the control 
' Parameter onChangeEvent: Javascript string to execute on event 'OnChange' 
' Parameter recordset: A database recordset with data. 
' RETURNS: [string] HTML select list 
function MakeOrgSelectList(name, onchangeEvent, recordset) 
    string html 
    html = "<select name='" & name & "' onchange='" & onchangeEvent & "'>" & vbCrLf 

    recordset.moveFirst 
    if not recordset.EOF AND not recordset.BOF then 
     html = html & "<option value='none' selected>Select one...</option>" & vbCrLf 

     do while not recordset.EOF 
      html = html & vbTab & "<option value='" & recordset("orgID") & "'>" & recordset("OrganizationName") & "</option>" & vbCrLf 
      'html = html & vbTab & "<option value='"&OrgCount&"'>"&recordset("OrganizationName")&"</option>" & vbCrLf 
      recordset.MoveNext 
     loop 
    else 
     html = html & "<option value='none' selected>no organizations found!</option>" & vbCrLf 
    end if 

    'return HTML string 
    MakeOrgSelectList = html 
End function 

至於javasscript,請使用element.options[element.selectedIndex].text來獲取選擇選項的文本。

function getSelectedOptionText(elementId) { 
    var element = document.getElementById(elementId); 
    if (element.selectedIndex == -1) 
     return null; 
    return element.options[element.selectedIndex].text; 
} 
+0

不得不稍微調整一下,但是「element.options [element.selectedIndex] .text」是關鍵。謝謝! – dcoughler

相關問題