Javascript checks whether the RadioButtonList option is selected

Source: Internet
Author: User

Write Javascript to determine whether the RadioButtonList option is selected. The effect is as follows:

Prepare the RadioButtonList Data source:
Cosmetic. vb Copy codeThe Code is as follows: 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, "moisturizing cream", "Olay", 50, "g ")
O. Add (c)
Dim c1 As New Cosmetic (2, "moisturizing cream", "Estee Lauder", 100, "g ")
O. Add (c1)
Dim c2 As New Cosmetic (3, "moisturizing cream", "Lancome", 80, "g ")
O. Add (c2)
Dim c3 As New Cosmetic (4, "moisturizing cream", "l'oreal", 60, "g ")
O. Add (c3)
Dim c4 As New Cosmetic (5, "moisturizing cream", "Babi polang", 120, "g ")
O. Add (c4)
Return o
End Function
End Class

End Namespace

Put a RadioButtonList control and a button in aspx:Copy codeThe Code is as follows: cosmetic: <asp: RadioButtonList ID = "RadioButtonListCosmetic" runat = "server" RepeatColumns = "10" RepeatDirection = "Horizontal"> </asp: RadioButtonList>
<Br/>
<Asp: Button ID = "Button1" runat = "server" Text = "Select"/>

In aspx. vb, bind the data source to RadioButtonList. Of course, the code below the bound data source must reference the namespace Imports Insus. NET.Copy codeThe Code is As follows: 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

The next step is to start the demo and write Javascript code:Copy codeThe Code is as follows: 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 ("select an option. ");
}
Return isSelect;
}
</Script>

Finally, write the client event for the Button of the ammonium Button.Copy codeThe Code is as follows: <asp: Button ID = "Button1" runat = "server" Text = "Select" OnClientClick = "return CheckIsSelected ()"/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.