前几天做了个省市区三级联动select,但不知道怎样传到后台写入数据库,现在终于找到解决方法:HiddenField控件
页面代码:
<div id="description"><br />所在地:
<select style="width:100px; " onchange="sech(this.id)" id="sheng">
<option value="province" >请选择省份</option>
</select>
<select onchange="sech(this.id)" id="shi">
<option value="city">请选择市区</option>
</select>
<select onchange="sech(this.id)" id="xian">
<option value="county">请选择县乡</option>
</select><asp:HiddenField ID="HiddenField1" runat="server" /><asp:HiddenField ID="HiddenField2" runat="server" /><asp:HiddenField ID="HiddenField3" runat="server" />
</div>
JS部分代码:
function sech(id) {//省市改变时触发
var aa = document.getElementById(id);
if(id=="sheng"){
var c = aa.selectedIndex;//获得改变后该省的索引号
document.getElementById("HiddenField1").value = aa.options[c].text;//将选中的省的text赋值给HiddenField1的Value;
}
if(id=="shi")
{
var d = aa.selectedIndex;
document.getElementById("HiddenField2").value = aa.options[d].text;
}
if (id == "xian") {
var e = aa.selectedIndex;
document.getElementById("HiddenField3").value = aa.options[e].text;
}
}
后台获取select选中代码:
Button1.Text = HiddenField1.Value+HiddenField2.Value+HiddenField3.Value;//获取JS赋值后的控件的值
这样就可以利用JS通过ASP控件HiddenField传递参数给后台,实现交互