我的页面上有两个HTML选择选项,一个(名为PrimaryArea)具有修复选项,另一个(名为SecondaryArea)将获得基于PrimaryArea选择的选项。
<select id="PrimaryArea" style="width: 100px" required tabindex="4" runat="server" onchange="setSecondaryItem()">
<option value="" disabled selected>Select....</option>
<option value="A">CityA</option>
<option value="B">CityB</option>
<option value="D">CityC</option>
<option value="E">CityD</option>
<option value="F">CityE</option>
</select>
<select id="SecondaryArea" style="width: 100px" tabindex="5" runat="server" required onchange="setSchoolItem()">
<option value="" disabled selected>select...</option>
</select>
Javascript代码:
function setSecondaryItem() {
var select = document.getElementById("<%= SecondaryArea.ClientID %>");
var length = select.options.length;
for (j = length - 1 ; j >= 1 ; j--) {
select.remove(j);
}
var js = JSON.stringify(<%= SecondaryTable %>);
var js2 = JSON.parse(js);
var len = js2.length;
var i = 0;
while (i < len) {
if (js2[i].cd.substring(0, 1) == document.getElementById("<%= PrimaryArea.ClientID %>").value) {
var new_option = new Option(js2[i].ref_desc, js2[i].cd);
select.options.add(new_option);
i += 1;
}
else {
i += 1;
}
}
select.options[0].selected = true;
}
function setSchoolItem() {
var select = document.getElementById("<%= School.ClientID %>");
var length = select.options.length;
for (j = length - 1 ; j >= 1 ; j--) {j
select.remove(j);
}
var js = JSON.stringify(<%= SchoolTable %>);
var js2 = JSON.parse(js);
var len = js2.length;
var i = 0;
while (i < len) {
if (js2[i].region_Cd == document.getElementById("<%= SecondaryArea.ClientID %>").value) {
var new_option = new Option(js2[i].temple_nm, js2[i].temple_id);
select.options.add(new_option);
i += 1;
}
else {
i += 1;
}
}
select.options[0].selected = true;
}
两者都很好。 我的问题是当我单击按钮处理后面的代码时,我使用C#,值不能正确返回。
protected void Submit_click(object sender, EventArgs args)
{
string area = PrimaryArea.Value; // --> area = "A"~"F"
string area2 = SecondaryArea.Value; // --area2 = 0 <-- should be value I put in but not 0
}
我做错了什么?
答案 0 :(得分:0)
应该是我放入的值而不是0
在JavaScript中,您必须使用索引而不是值来设置SecondArea。调试JavaScript以查看位置。