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()" />

(0)

相关推荐

  • ASP.NET中RadioButtonList绑定后台数据后触发点击事件

    本文实例为大家分享了RadioButtonList绑定后台数据,触发点击事件的方法 首先前台页面放置一个RadioButtonList 控件 <asp:RadioButtonList runat="server" ID="RadioButtonList1" BorderStyle="None" RepeatColumns="3" CssClass="" RepeatLayout="Flow&

  • ASP.NET控件之RadioButtonList详解

    "RadioButtonList"控件表示一个封装了一组单选按钮控件的列表控件. 可以使用两种类型的 ASP.NET 控件将单选按钮添加到网页上:各个"RadioButton"控件或一个"RadioButtonList"控件.这两类控件都允许用户从一小组互相排斥的预定义选项中进行选择.使用这些控件,可定义任意数目的带标签的单选按钮,并将它们水平或垂直排列. 命名空间:System.Web.UI.WebControls 程序集:System.Web

  • js获取RadioButtonList的Value/Text及选中值等信息实现代码

    HTML代码 复制代码 代码如下: <asp:RadioButtonList ID="rbtnCompany" runat="server" RepeatColumns="4" RepeatDirection="horizontal"> <asp:ListItem Value="1" Text="A"></</SPAN>asp:ListItem&

  • RadioButtonList绑定图片及泛型Dictionary应用

    本博文是让你学会读取站点某一目录的图片,掌握LINQ与泛型Dictionary<TKey,TValue>的使用. 首先准备好几张图片存在站点某一目录之下,本例中的存储图片的目录名为MsSiteImages,图片你可以从微软网站下载http://windows.microsoft.com/en-US/windows/home 我们写一个泛型数据集,将存储目录的图片信息: 复制代码 代码如下: View Code private Dictionary<int, string> GetD

  • 如何为CheckBoxList和RadioButtonList添加滚动条

    如何给CheckBoxList和RadioButtonList添加滚动条? 继承基类CheckBoxList和RadioButtonList,添加滚动属性,重写Render方法即可. 属性列表: #region 滚动控制 private bool _ShowScrollBar = false; /// <summary> /// 显示滚动条 /// </summary> [ System.ComponentModel.Description("是否显示显示滚动条"

  • jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值

    -.获取dropdownlist的text(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签) $("#ddlList option:selected").text() 二.获取dropdownlist的value(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签) $("#ddlList").val() 三.获取radiobuttonlist的t

  • ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法

    这三个控件都有一个Items集合,可以用 RepeatLayout 和 RepeatDirection 属性来控制列表的呈现形式.如果 RepeatLayout 的值为 Table,那么将在表中呈现列表.如果设置成 Flow,那么将在没有任何表结构的情况下呈现列表.默认情况下,RepeatDirection 的值为 Vertical.将此属性设置成 Horizontal 将会使列表水平呈现. RadioButtonList:控件提供已选中一个选项的单项选择列表(数据源单选).与其他列表控件相似,

  • JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结

    一: DropDownList ------------------------------------------------------------------------------------------- 在使用 JQuery 进行遍历操作时, $("input").each(function(i) { ...... } 当操作对象的类型为 dropdownlist时:(备注:在firefox下DropDownList的类型为"select-one") 获

  • ASP.NET jQuery 实例16 通过控件CustomValidator验证RadioButtonList

    界面代码: 复制代码 代码如下: <form id="form1" runat="server"> <div align="center"> <fieldset style="width: 350px; height: 200px;"> <table border="0" cellpadding="3" cellspacing="3&q

  • jquery判断RadioButtonList和RadioButton中是否有选中项示例

    复制代码 代码如下: <pre class="html" name="code"> <%--Body 代码--%> <div> <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem Value="A">a</asp:ListItem> <a

随机推荐