Reading CheckBoxes and Radio Buttons

 


Input tags with the type attribute checkbox can be grouped like radio buttons so that several checkboxes have the same name. However, any number of checkboxes can be selected by the user. Working with checkboxes in JavaScript is similar to but not exactly the same as working with radio buttons. Here is a short example that demonstrates getting values from checkboxes and radio buttons in the same form:


Music Survey

I enjoy:ClassicalChamberJazzRockPunk
Favourite Performer:
AitkenColtraneJulliardKronosWaits


Getting all the values

A series of checkboxes with the same name are reflected as an array of checkbox objects. In this example each check box input tag is named Music but has a different value. Since there are identically named input tags of the same type, an array of checkbox objects named Music is created. When the button in this form is pressed, it passes a reference to the form to a function named, in this case, showBoxes(frm). The function loops through the checkbox array within the form. To access the check box array from the form reference it is only necessary to append the name of the checkboxes to the form reference:

frm.Music

For example to access the number of elements in the array:

frm.Music.length

or if the variable i contains the index of one of the elements, here is how you would check to see if that element had been checked:

if (frm.Music[i].checked)

or get that element's value:

frm.Music[i].value

Since many values may be checked, this example gathers the values from each by appending each value on to the end of a string. Since the string is eventually displayed in an alert dialog each value is separated by a "\n", or new line character. In this case the string is named message

message = message + frm.Music[i].value + "\n"

Here is the complete page:

<HTML>
<HEAD>
<TITLE>JavaScript - Reading Checkboxes and Radio Buttons</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function showBoxes(frm){
   var message = "Your chose:\n\n"

   //For each checkbox see if it has been checked, record the value.
   for (i = 0; i < frm.Music.length; i++)
      if (frm.Music[i].checked){
         message = message + frm.Music[i].value + "\n"
      }

   //For each radio button if it is checked get the value and break.
   for (var i = 0; i < frm.Performer.length; i++){
      if (frm.Performer[i].checked){
         message = message + "\n" + frm.Performer[i].value
         break
      }
   }
   alert(message)
}

//NOTE THE onClick="" empty event handlers in the checkbox and radio input fields below
//are there to fix a bug in Netscape 2.0X. Placing an onClick handler in imput fields fixes
//a bug where input objects inside tables are reflected more than once.

//-->
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#ffffff">
<FORM NAME="boxes">
<HR size=1 noshade>
<h2>Music Survey </h2>
<TABLE BORDER="1" CellSpacing="0" CellPadding="6">
<TR>
  <TD align="right">I enjoy:</TD>
  <TD><INPUT TYPE="Checkbox" NAME="Music"  VALUE="Classical" onClick="" CHECKED>Classical</TD>
  <TD><INPUT TYPE="Checkbox" NAME="Music"  VALUE="Chamber"   onClick="">Chamber</TD>
  <TD><INPUT TYPE="Checkbox" NAME="Music"  VALUE="Jazz"      onClick="" CHECKED>Jazz</TD>
  <TD><INPUT TYPE="Checkbox" NAME="Music"  VALUE="Rock"      onClick="">Rock</TD>
  <TD><INPUT TYPE="Checkbox" NAME="Music"  VALUE="Punk"      onClick="">Punk</TD>
</TR>
<TR>
  <TD><div align="right">Favourite Performer:</div></TD>
  <TD><INPUT TYPE="radio" NAME="Performer" VALUE="Aitken"    onClick="">Aitken</TD>
  <TD><INPUT TYPE="radio" NAME="Performer" VALUE="Coltrane"  onClick="" CHECKED>Coltrane</TD>
  <TD><INPUT TYPE="radio" NAME="Performer" VALUE="Julliard"  onClick="">Julliard</TD>
  <TD><INPUT TYPE="radio" NAME="Performer" VALUE="Kronos"    onClick="">Kronos</TD>
  <TD><INPUT TYPE="radio" NAME="Performer" VALUE="Waits"     onClick="">Waits</TD>
</TR>
</TABLE>
<P>
<INPUT TYPE="Button" VALUE="Tell Us What You Like!" onClick="showBoxes(this.form)">
</P>
</FORM>
</BODY>
</HTML>

转载于:https://www.cnblogs.com/hannover/p/4210070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值