zoukankan      html  css  js  c++  java
  • Javascript Get or Set Checked Radio Value

     

    Description

    This pair of Javascript function can get or set the checked value of a group of radio buttons.  These functions are specially designed for dynamic pages, and work without error with zero, one, or more radio buttons.  Also, because the radio length is saved before looping, this function is much faster.  Finally, the functions are granted to the public domain.

    Source Code

    // return the value of the radio button that is checked
    // return an empty string if none are checked, or
    // there are no radio buttons
    function getCheckedValue(radioObj) {
    	if(!radioObj)
    		return "";
    	var radioLength = radioObj.length;
    	if(radioLength == undefined)
    		if(radioObj.checked)
    			return radioObj.value;
    		else
    			return "";
    	for(var i = 0; i < radioLength; i++) {
    		if(radioObj[i].checked) {
    			return radioObj[i].value;
    		}
    	}
    	return "";
    }
    
    // set the radio button with the given value as being checked
    // do nothing if there are no radio buttons
    // if the given value does not exist, all the radio buttons
    // are reset to unchecked
    function setCheckedValue(radioObj, newValue) {
    	if(!radioObj)
    		return;
    	var radioLength = radioObj.length;
    	if(radioLength == undefined) {
    		radioObj.checked = (radioObj.value == newValue.toString());
    		return;
    	}
    	for(var i = 0; i < radioLength; i++) {
    		radioObj[i].checked = false;
    		if(radioObj[i].value == newValue.toString()) {
    			radioObj[i].checked = true;
    		}
    	}
    }
    

    Example

    Use the view source command in your browser to see how the code below works.  Note the use of the label HTML tag with a style that gives it a border and background color. You should always use the label tag with radio buttons. This signals to the user that the whole boxed area may be clicked to set the radio button, rather than just the tiny circle.

    If you are using a scripting language like PHP, ColdFusion, or ASP, then write a function that prints your radio buttons from an array.  It can automatically write the for and id properties by concatenating the radio name and value.

  • 相关阅读:
    Cefsharp 75 设置代理
    C# Winform 执行JS脚本
    Cefsharp 75 为每个实例单独设置缓存目录
    QQ邮箱发送邮件,出现mail from address must be same as authorization user错误
    软件卸载后自动重装,服务器中了流氓软件解决方法
    win版宝塔更新后,宝塔无法启动(修复办法)
    C的typedef
    为Arch Linux安装桌面
    在Simplicity Studio下创建适用于EFR32的工程项目
    在VMWare上安装Arch Linux
  • 原文地址:https://www.cnblogs.com/hannover/p/4210047.html
Copyright © 2011-2022 走看看