【发布时间】:2018-03-26 17:49:45
【问题描述】:
我意识到这个问题已经以各种形式被问过很多次,但我还没有找到解决我的问题的解决方案。
我正在写一个网站来记录死者的详细信息。我的网页有一个标题下拉框和一个性别下拉框。
在数据库中,这些是链接的,我需要根据标题设置性别。
我有一个列表框,其中包含标题 ID/性别 ID 链接以及使用此列表框从标题中查找性别的功能。
<asp:Label CssClass="LegacyLabel">Title</asp:Label>
<asp:ListBox ID="TitleSex" runat="server" CssClass="hidethis" SelectionMode="Single"></asp:ListBox>
<asp:DropDownList ID="LegatorTitle" runat="server" CssClass="LegacyDDL" TabIndex="2" onchange="setSex()"></asp:DropDownList>
<asp:Label CssClass="LegacyLabel">Sex</asp:Label>
<asp:DropDownList ID="LegatorSex" runat="server" CssClass="LegacyDDL" TabIndex="5"></asp:DropDownList>
<script>
function setSex() {
var t = document.getelementbyid("<%=LegatorTitle.ClientID %>");
var ts = document.getElementById("<%=TitleSex.ClientID %>");
var s = document.getElementById("<%=LegatorSex.ClientID %>");
//get title selectd value
var tValue = t.options[t.selectedIndex].value;
var opts = ts.options;
//set the title sex selected value
ts.selectedItem.selected = false;
for (var z0 = 0; z0 < opts.length; z0++) {
if (opts[z0].value == tValue) {
ts.selectedIndex = z0;
break;
}
}
//get the title sex selected text
var tsText = ts.options[ts.selectedIndex].text;
opts = s.options;
//set the sex selected value
s.selectedItem.selected = false;
for (z0 = 0; z0 < opts.length; z0++) {
if (opts[z0].value == tsText) {
s.selectedIndex = z0;
break;
}
}
}
</script>
下拉列表和列表框是通过使用数据表的 Web 服务设置的
dataTable = fcws.GetTitles(UNAME, PWORD)
LegatorTitle.Items.Clear()
LegatorTitle.DataSource = dataTable
LegatorTitle.DataTextField = "TitleName"
LegatorTitle.DataValueField = "TitleId"
LegatorTitle.DataBind()
TitleSex.Items.Clear()
TitleSex.DataSource = dataTable
TitleSex.DataTextField = "SexId"
TitleSex.DataValueField = "TitleId"
TitleSex.DataBind()
dataTable = fcws.GetTitles(UNAME, PWORD)
LegatorTitle.Items.Clear()
LegatorTitle.DataSource = dataTable
LegatorTitle.DataTextField = "TitleName"
LegatorTitle.DataValueField = "TitleId"
LegatorTitle.DataBind()
'sex
dataTable = fcws.GetSexes(UNAME, PWORD)
LegatorSex.Items.Clear()
LegatorSex.DataSource = dataTable
LegatorSex.DataTextField = "SexName"
LegatorSex.DataValueField = "SexId"
LegatorSex.DataBind()
我还有案例打开和案例关闭日期和状态(带有打开和关闭选项的下拉列表)
当我尝试加载打开的箱子时,一切正常
当我尝试加载已关闭的案例时,我收到以下错误
Server Error in '/' Application
Cannot have multiple items selected in a DropDownList
Source error
Line 245: var t = document.getelementbyid("<%=LegatorTitle.ClientID %>");
Line 246: var ts = document.getElementById("<%=TitleSex.ClientID %>");
--> Line 247: var s = document.getElementById("<%=LegatorSex.ClientID %>"); <---
Line 248: //get title selected value
Line 249: var tValue = t.options[t.selectedIndex].value;
(--> ...
I cannot post images yet so here is a link to the error
我不明白的是:-
- 首先为什么会出现错误
- 为什么错误只发生在已关闭的案例中
- 为什么,当我换行时 'var s = document.getElementById("");'到 'var s = document.getElementById("LegatorSex");',错误移动到行 var ts = document.getElementById(""); (如果我更改了上面代码中的所有行,错误将移动到带有 的上一行)
我检查了有帮助的回复(这就是为什么我在绑定列表之前清除列表以及为什么我在重新设置之前将 selectedItem.selected 设置为 false)完全没有帮助(错误非常具体:你下拉列表中不能有多个选择。找到另一个更适合您需要的控件!)但似乎没有任何效果。
【问题讨论】:
-
第 10 行的开始 - 第 245 行:
document.getelementbyid应该是document.getElementById
标签: javascript html asp.net