您现在的位置:龙卷风首页 ›› 网络编程 ›› 阅读文章

asp中获取select控件的value和text

select option value text

在asp.net中,可以通过服务器控件DropDownList,很方便的获取select控件的value和text,但是在asp这种不能回发的网页编程中,就只能借助于hidden filed和javascript了。

思路:当select选项改动时,把select的text赋值给隐藏域。

解决方法:使用jquery可以使问题更简单一些。

1、设置select的id为selectA,隐藏域的id为hiddenA,于是可以写出调用的函数:

js:

function SetValue(){
$('#selectA option').each(function(){
if(this.selected)
$('#hiddenA')[0].value = this.innerText;
});
}

2、设置select的onchange事件:

onchange="SetValue();"

这样就可以在服务器端使用asp代码来分别接收select的value和text了。

3、asp代码:

selectValue = Request.Form("selectA")
selectText = Request.Form("hiddenA")

注意,innerText只能在IE中起作用,再firefox中要用textContent。

作者 不见不散 本文仅代表作者观点,与龙卷风资讯网立场无关。

我来说两句

内容/Content