javascript - 如何保存下拉列表中选择的所有值
问题描述
我有一个下拉列表,我使用了一些 JS 来更改它,我使用了这个站点(我使用了多个 selected2):链接
有了这个,我想获取下拉列表中选择的所有值并将它们插入到我的数据库中,但为此我必须选择所有值,但我只得到其中的 1 个。
网
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="MenuBar.css" />
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<title></title>
</head>
<body style="background-color: #fff">
<form id="form1" runat="server">
<div class="principal">
<div class="txtbox">
<div class="rz-fieldset-content">
<asp:Button ID="ButtonInsert" runat="server" Text="Inserir" OnClick="ButtonInsert_Click" />
<asp:Button ID="ButtonChange" runat="server" Text="Alterar" OnClick="ButtonChange_Click" />
<fieldset id="fieldset" runat="server">
<legend>Encomendas</legend>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Style="display: block; color: black; font-size: 15px;" Text="Selecione o BL"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownListBL" CssClass="txt" Style="padding-right: 10px; color: grey;" runat="server">
<asp:ListItem Text="- -" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Style="display: block; color: black; font-size: 15px;" Text="Selecione a(s) encomenda(s)"></asp:Label>
</td>
<td>
<asp:DropDownList ID="multiple" CssClass="txt" Style="padding-right: 10px; color: grey; width: 100%" runat="server" multiple>
</asp:DropDownList>
</td>
</tr>
</table>
</fieldset>
<asp:Button ID="ButtonCreate" runat="server" Text="Create" OnClick="ButtonCreate_Click" />
<asp:Button ID="ButtonBack" runat="server" Text="Back" OnClick="ButtonBack_Click" />
<br />
<asp:Label ID="Changed" runat="server" Text="Alterado com sucesso!"></asp:Label>
</div>
</div>
</div>
<!-- jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Select2 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<script>
$("#DropDownListBL").select2({
/*placeholder: "Select a programming language",*/
allowClear: true
});
$("#multiple").select2({
/*placeholder: "Select a programming language",*/
allowClear: true
});
</script>
</form>
</body>
</html>
CS
protected void ButtonCreate_Click(object sender, EventArgs e)
{
string teste = multiple.SelectedValue;
con.Open();
if(DropDownListBL.SelectedIndex != 0 && multiple.SelectedIndex != 0)
{
SqlCommand cmd = new SqlCommand("UpdtEncomenda", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@BL", DropDownListBL.SelectedValue));
cmd.Parameters.Add(new SqlParameter("@No_", multiple.SelectedValue));
int value = cmd.ExecuteNonQuery();
ButtonBack.Visible = true;
Changed.Visible = true;
multiple.SelectedIndex = 0;
DropDownListBL.SelectedIndex = 0;
}
con.Close();
}
解决方案
试试这个来选择多个值:
<asp:ListBox ID="lstEmployee" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="Nikunj Satasiya" Value="1" />
<asp:ListItem Text="Dev Karathiya" Value="2" />
<asp:ListItem Text="Hiren Dobariya" Value="3" />
<asp:ListItem Text="Vivek Ghadiya" Value="4" />
<asp:ListItem Text="Pratik Pansuriya" Value="5" />
</asp:ListBox>
推荐阅读
- powershell - PowerShell ForEach 循环中可以有多个 IN 条件吗?
- php - 如何在 PHP 中对大/高精度浮点数进行基础转换?
- python - 文本分类与推荐
- c - MPI OpenMP Hybrid 比 Pure MPI 慢
- elasticsearch - Elasticsearch,Kibana看起来很慢,如何拆分索引?
- javascript - Materialise Sidenav 不适用于 iOS 或 iPhone,但适用于所有其他设备
- python - 从变量中提取特定字符串
- qt - 当我使用 setUseOpenGL(true) 时,QChartSeries 的某些属性将被禁用
- c# - 仅将包含 3 个文件的特定文件夹复制到新文件夹
- angular - 在 p-dialog 上打开花式框时关闭 PRIMENG 的 p-dialog