如何从选择的选项中获取值

时间:2018-05-23 14:42:45

标签: javascript asp-classic

我有一个Users表,其中包含User_id,Firstname,Lastname和Role。我喜欢创建一个经典的ASP页面来选择一个选项时显示值。每当我从下拉框中选择用户名时,它都会在下面显示其值。我试过了,但是当我在下拉列表中选择用户名时,它没有做任何事情。

Here is my code:
User_id   Firstname   Lastname   Role
-------------------------------------
1         Jenny       Rogers     A
2         Jenny2      Rogers2    B
1         Jenny       Rogers     B
2         Jenny2      Rogers2    A
3         Jenny3      Rogers3    C

ASP代码:



<form action="" method="post" id="newMenu" name="frmReport" onSubmit="return validateForm(this)">
  <table>
    <thead>
      <tr>
        <td>
          <strong>New User: </strong>
          <select name="UserID">
            <option value="">All Users</option>
            <% sSQL = "SELECT DISTINCT User_id, Lastname, Firstname FROM User "
                    objRS.Open sSQL, objConn
                    Do Until objRS.EOF
                        Response.Write("<option value='" & objRS("User_id") & "'>" & objRS("Lastname") & ", " & objRS("Firstname") & "</option>")
                    objRS.MoveNext
                    Loop
                    objRS.Close
                %>
          </select>
          <% 
            If Len("UserID") > 0 Then
                                    sSQL = "SELECT User_id, Role FROM Users WHERE User_id = '" & request.Form("UserID") & "' ; "
                                    objRS.Open sSQL, objConn
                    Do Until objRS.EOF
                %>
            <input type="text" name="Role" value="<%= objRS(" Role ") %>">
            <%  objRS.MoveNext
                    Loop
                    objRS.Close
    End If
                %>
              <input type="hidden" name="FormSource" value="SubmitForm">
              <input type="submit" value="Submit Form">
              </th>
      </tr>
  </table>
</form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-1)

也许这就是你想要的

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Connections/BUYIN.asp" -->
<%
''''''Recordset with Names
Dim objRS
Dim objRS_cmd
Dim objRS_numRows

Set objRS_cmd = Server.CreateObject ("ADODB.Command")
objRS_cmd.ActiveConnection = MM_BUYIN_STRING
objRS_cmd.CommandText = "SELECT id, FirstName, LastName FROM dbo.BI_Users WHERE firstname like '%test%'" 
objRS_cmd.Prepared = true

Set objRS = objRS_cmd.Execute
objRS_numRows = 0
%>
<!--Results -->
<% If Request.Form("selectID") <> "" Then %>
<%
Dim rsResults__getID
rsResults__getID = "0"
If (Request.Form("selectID") <> "") Then 
  rsResults__getID = Request.Form("selectID")
End If
%>
<%
Dim rsResults
Dim rsResults_cmd
Dim rsResults_numRows

Set rsResults_cmd = Server.CreateObject ("ADODB.Command")
rsResults_cmd.ActiveConnection = MM_BUYIN_STRING
rsResults_cmd.CommandText = "SELECT id, FirstName, LastName, fkRoleID FROM dbo.BI_Users WHERE id = ?" 
rsResults_cmd.Prepared = true
rsResults_cmd.Parameters.Append rsResults_cmd.CreateParameter("param1", 5, 1, -1, rsResults__getID) ' adDouble

Set rsResults = rsResults_cmd.Execute
rsResults_numRows = 0
%>
<% End If %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="testasp2.asp">
<select name="selectID" id="selectID">
    <%
While (NOT objRS.EOF)
%>
    <option value="<%=(objRS.Fields.Item("id").Value)%>" <%If (Not isNull(Request.Form("selectID"))) Then If (CStr(objRS.Fields.Item("id").Value) = CStr(Request.Form("selectID"))) Then Response.Write("selected=""selected""") : Response.Write("")%> ><%=(objRS.Fields.Item("LastName").Value)%></option>
    <%
  objRS.MoveNext()
Wend
If (objRS.CursorType > 0) Then
  objRS.MoveFirst
Else
  objRS.Requery
End If
%>
  </select>
  <input type="submit" name="button" id="button" value="Submit" />
</form>
<hr />
<% If Request.Form("selectID") <> "" Then %>
<% If Not rsResults.EOF Or Not rsResults.BOF Then %>
<p><%=(rsResults.Fields.Item("id").Value)%> </p>
<p><%=(rsResults.Fields.Item("FirstName").Value)%> </p>
<p><%=(rsResults.Fields.Item("LastName").Value)%> </p>
<p><%=(rsResults.Fields.Item("fkRoleid").Value)%> </p>

<%
rsResults.Close()
Set rsResults = Nothing
%>
<% End If %>
<% Else %>
<%= "No Results Yet" %>
<% End If  %>
</body>
</html>
<%
objRS.Close()
Set objRS = Nothing
%>