欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javascript判断是否有对RadioButtonList选项选择

程序员文章站 2024-03-05 19:41:49
写javascript来判断是否有对radiobuttonlist选项选择,效果如下: 准备好radiobuttonlist数据源: cosmetic.vb 复制代码 代码...
写javascript来判断是否有对radiobuttonlist选项选择,效果如下:
javascript判断是否有对RadioButtonList选项选择
准备好radiobuttonlist数据源:
cosmetic.vb
复制代码 代码如下:

imports microsoft.visualbasic
namespace insus.net
public class cosmetic
private _id as integer
private _type as string
private _name as string
private _weight as decimal
private _um as string
public property id as integer
get
return _id
end get
set(value as integer)
_id = value
end set
end property
public property type as string
get
return _type
end get
set(value as string)
_type = value
end set
end property
public property name as string
get
return _name
end get
set(value as string)
_name = value
end set
end property
public property weight as decimal
get
return _weight
end get
set(value as decimal)
_weight = value
end set
end property
public property um as string
get
return _um
end get
set(value as string)
_um = value
end set
end property
public sub new()
end sub
public sub new(id as integer, type as string, name as string, weight as decimal, um as string)
me._id = id
me._type = type
me._name = name
me._weight = weight
me._um = um
end sub
public function getdata() as list(of cosmetic)
dim o as new list(of cosmetic)
dim c as new cosmetic(1, "滋润霜", "玉兰油", 50, "g")
o.add(c)
dim c1 as new cosmetic(2, "滋润霜", "雅诗兰黛", 100, "g")
o.add(c1)
dim c2 as new cosmetic(3, "滋润霜", " 兰蔻", 80, "g")
o.add(c2)
dim c3 as new cosmetic(4, "滋润霜", "欧莱雅", 60, "g")
o.add(c3)
dim c4 as new cosmetic(5, "滋润霜", "芭比波朗", 120, "g")
o.add(c4)
return o
end function
end class

end namespace

在aspx放一个radiobuttonlist控件和一个铵钮:
复制代码 代码如下:

化妆品:<asp:radiobuttonlist id="radiobuttonlistcosmetic" runat="server" repeatcolumns="10" repeatdirection="horizontal"></asp:radiobuttonlist>
<br />
<asp:button id="button1" runat="server" text="select" />

在aspx.vb中,为radiobuttonlist绑定数据源,当然绑定数据源下面的代码中,还得引用命名空间 imports insus.net
复制代码 代码如下:

protected sub page_load(sender as object, e as eventargs) handles me.load
if not ispostback then
data_binding()
end if
end sub
private sub data_binding()
dim objcosmetic as new cosmetic()
me.radiobuttonlistcosmetic.datasource = objcosmetic.getdata()
me.radiobuttonlistcosmetic.datatextfield = "name"
me.radiobuttonlistcosmetic.datavaluefield = "id"
me.radiobuttonlistcosmetic.databind()
end sub

接下来是演示开始,写javascript代码:
复制代码 代码如下:

view code
<script type="text/javascript">
function checkisselected() {
var rbl = document.getelementbyid("<%=radiobuttonlistcosmetic.clientid%>");
var radio = rbl.getelementsbytagname("input");
var isselect = false;
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
isselect = true;
break;
}
}
if (!isselect) {
alert("请选择一个选项。");
}
return isselect;
}
</script>

最后是为铵钮button写客户端事件
复制代码 代码如下:

<asp:button id="button1" runat="server" text="select" onclientclick="return checkisselected()" />