我在頁面上有兩個HTML選擇選項,其中一個(名爲PrimaryArea)具有修復選項,另一個(名爲SecondaryArea)將根據PrimaryArea選項獲取選項。c#無法獲得代碼後面的HTML選擇選項值
<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
}
我做錯了什麼?
我找到了原因,但我不知道爲什麼會發生。我發現SecondaryArea項目的長度返回1.但實際上SecondaryArea有兩個以上的項目。 –