javascript判斷是否有對RadioButtonList選項選擇
更新時間:2013年01月14日 10:32:18 作者:
寫個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()" />

準備好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()" />
您可能感興趣的文章:
相關文章
VS2015 Update2 構建 Android 程序問題匯總
這篇文章主要介紹了VS2015 Update2 構建 Android 程序問題匯總的相關資料,需要的朋友可以參考下2016-07-07ABP框架中導航菜單的使用及JavaScript API獲取菜單的方法
ABP框架是基于ASP.NET的Web開發(fā)框架,其中包含基本的菜單項可供調用,特別是自動生成的js API使得能夠在客戶端獲取菜單,這里我們就來看一下ABP框架中導航菜單的使用及JavaScript API獲取菜單的方法2016-06-06.NET Core利用動態(tài)代理實現AOP(面向切面編程)
用動態(tài)代理可以做AOP(面向切面編程),進行無入侵式實現自己的擴展業(yè)務,調用者和被調用者之間的解耦,提高代碼的靈活性和可擴展性。本文將為大家詳細介紹實現的方法,感興趣的可以學習一下2022-01-01在vs2008中使用AJAX開發(fā).net 2.0下的Web程序的方法
最近做項目需要用到AJAX,但是工作需要必須使用.net 2.0版本;但發(fā)現如果項目為.net2.0版本則沒有Ajax(web.config已經配置上ajax) ,、工具欄中并不出現Ajax選項卡. 而且新建頁面也沒有AJAX頁面;后來查了很多資料 發(fā)現了一種解決方法2011-06-06