zoukankan      html  css  js  c++  java
  • How to get the value of a form element : check box and radio button

    Getting a radio element and it’s checked value

    Radio button

    Radio buttons in a form can be grouped together using the same name attribute, such that, if one of them is checked, all the others are automatically un-checked.

    Let us look at an example:

    <input type="radio" id="work_abroad_y" name="work_abroad" value="y" /><label for="work_abroad_y">Yes</label>
    <input type="radio" id="work_abroad_n" name="work_abroad" value="n" /><label for="work_abroad_n">No</label>
    

    In order to retrieve the value of the checked radio button, we can write a simple JavaScript function:

    function getRadioCheckedValue(radio_name)
    {
       var oRadio = document.forms[0].elements[radio_name];
    
       for(var i = 0; i < oRadio.length; i++)
       {
          if(oRadio[i].checked)
          {
             return oRadio[i].value;
          }
       }
    
       return '';
    }
    

    We obtain a reference to the group of radio buttons using the name of the group, and then iterate over all the radio button objects to test which one of them is checked.

    Check box

    A check box in a form has only two states (checked or un-checked) and is independent of the state of other check boxes in the form, unlike radio buttons which can be grouped together under a common name. Let us look at the HTML code showing two check boxes:

    <input type="checkbox" name="email_alerts" id="chk_email_alerts" value="ON" checked><label for='chk_email_alerts'>Email me matching jobs everyday</label>
    
    <input type="checkbox" name="recruiter_contact" id="chk_recruiter_contact" value="ON"><label for='chk_recruiter_contact'>Enable Recruiters to directly contact me</label>
    

    In order to access these checkboxes, their values, and their states, we can use the following javascript function:

    function testCheckbox(oCheckbox)
    {
        var checkbox_val = oCheckbox.value;
        if (oCheckbox.checked == true)
        {
            alert("Checkbox with name = " + oCheckbox.name + " and value =" +
                    checkbox_val + " is checked");
        }
        else
        {
            alert("Checkbox with name = " + oCheckbox.name + " and value =" +
                  checkbox_val + " is not checked");
        }
    }
    

    We can then use the function this way:

    oCheckBox1 = oForm.elements["email_alerts"];
    oCheckBox2 = oForm.elements["recruiter_contact"];
    
    testCheckbox(oCheckBox1);
    testCheckbox(oCheckBox2);
    

    Example

    See the demo

    The textarea for ‘work permit’ gets disabled if ‘No’ is chosen for ‘Willing to work abroad ?’ and vis-versa We write onClick event handler for each of the options. First have a look at the HTML:

    Willing to work abroad ?
    <input type="radio" name="work_abroad" id="work_abroad_y" value="y" onClick="enableElement(this.form.work_permit);"><label for='work_abroad_y'>Yes</label>
    <input type="radio" name="work_abroad" id="work_abroad_n" value="n" onClick="disableElement(this.form.work_permit);"><label for='work_abroad_n'>No</label>
    
    <label for="work_permit">Information about acquiring work permit / visa: </label><textarea name="work_permit" rows="3" cols="35"></textarea>
    

    The JavaScript code:

    function disableElement(obj)
    {
         obj.value = ' - N.A. - ';
         obj.disabled = true;
    }
    
    function enableElement(obj)
    {
         obj.value = '';
         obj.disabled = false;
    }
    

    Download the code

    Prev: How to get the value of a form element : Drop downs and lists

  • 相关阅读:
    求幂运算、多项式乘法及Horner法则的应用
    JAVA泛型中的类型擦除及为什么不支持泛型数组
    关于递归的理解及递归表达式复杂度分析(以求解最大公约数为例)
    随机序列生成算法---生成前N个整数的一组随机序列
    Windows 与 Linux下关于端口不能访问的问题
    Netty 实现HTTP文件服务器
    字符数组转换成数字
    字符串反转的进一步应用----单词反转
    递归算法编程整数因子分解问题的递归算法
    数据返回[数据库基础]——图解JOIN
  • 原文地址:https://www.cnblogs.com/hannover/p/4210090.html
Copyright © 2011-2022 走看看