js获得radio、checkbox、select选中值

 var JS = {
        IsNullOrEmpty:function(str){ 
            if(typeof(str)=="undefined") return true; 
            if(str==null) return true;
            if(str=="") return true;
            return false;
        },//IsNullOrEmpty
        GetRadioValue:function(radioName)
        {
            var radios = document.getElementsByName(radioName);
            for(var i = 0 ; i< radios.length; i++)
            {
                var radio = radios.item(i);
                if(radio.checked)
                {
                    if(JS.IsNullOrEmpty(radio.value)) return undefined;
                    return radio.value;
                }
            }
            return undefined;
        },//GetRadioValue
        GetCheckboxValue:function(checkboxName)
        {
            var checkboxs = document.getElementsByName(checkboxName);
            var ck = new Array();
            var j  =  0;
            for(var i = 0 ; i< checkboxs.length; i++)
            {
                var checkbox = checkboxs[i];
                if(checkbox.checked)
                {
                    if(JS.IsNullOrEmpty(checkbox.value)) return undefined;
                    ck[j] = checkbox.value;
                    j++;
                }
            }
            return j == 0 ? undefined : ck.join();
        },//GetCheckboxValue
        GetSelectValue:function(selectId)
        {
            var select = document.getElementById(selectId);
            var options = select.options;
            for(var i = 0 ; i<options.length ;i++)
            {
                var option = options[i];
                if(option.selected)
                {
                    if(JS.IsNullOrEmpty(option.value)) return undefined;
                    return option.value;
                }
            }
            return undefined;
        }//GetSelectValue
}

压缩版

======================

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('1 d={b:7(6){3(u(6)=="4")2 k;3(6==v)2 k;3(6=="")2 k;2 x},w:7(q){1 m=e.n(q);l(1 i=0;i<m.h;i++){1 8=m.t(i);3(8.p){3(d.b(8.5))2 4;2 8.5}}2 4},B:7(o){1 g=e.n(o);1 f=D E();1 j=0;l(1 i=0;i<g.h;i++){1 a=g[i];3(a.p){3(d.b(a.5))2 4;f[j]=a.5;j++}}2 j==0?4:f.y()},C:7(r){1 s=e.z(r);1 9=s.9;l(1 i=0;i<9.h;i++){1 c=9[i];3(c.A){3(d.b(c.5))2 4;2 c.5}}2 4}}',41,41,'|var|return|if|undefined|value|str|function|radio|options|checkbox|IsNullOrEmpty|option|JS|document|ck|checkboxs|length|||true|for|radios|getElementsByName|checkboxName|checked|radioName|selectId|select|item|typeof|null|GetRadioValue|false|join|getElementById|selected|GetCheckboxValue|GetSelectValue|new|Array'.split('|'),0,{}))

用法:

======================

JS.GetRadioValue('Radio');

JS.GetCheckboxValue('checkbox[]');

JS.GetSelectValue('Select');


例子:

=====================

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Use JavaScript to get radio and select value</title>
    <script>
    var JS = {
        IsNullOrEmpty:function(str){ 
            if(typeof(str)=="undefined") return true; 
            if(str==null) return true;
            if(str=="") return true;
            return false;
        },//IsNullOrEmpty
        GetRadioValue:function(radioName)
        {
            var radios = document.getElementsByName(radioName);
            for(var i = 0 ; i< radios.length; i++)
            {
                var radio = radios.item(i);
                if(radio.checked)
                {
                    if(JS.IsNullOrEmpty(radio.value)) return undefined;
                    return radio.value;
                }
            }
            return undefined;
        },//GetRadioValue
        GetCheckboxValue:function(checkboxName)
        {
            var checkboxs = document.getElementsByName(checkboxName);
            var ck = new Array();
            var j  =  0;
            for(var i = 0 ; i< checkboxs.length; i++)
            {
                var checkbox = checkboxs[i];
                if(checkbox.checked)
                {
                    if(JS.IsNullOrEmpty(checkbox.value)) return undefined;
                    ck[j] = checkbox.value;
                    j++;
                }
            }
            return j == 0 ? undefined : ck.join();
        },//GetCheckboxValue
        GetSelectValue:function(selectId)
        {
            var select = document.getElementById(selectId);
            var options = select.options;
            for(var i = 0 ; i<options.length ;i++)
            {
                var option = options[i];
                if(option.selected)
                {
                    if(JS.IsNullOrEmpty(option.value)) return undefined;
                    return option.value;
                }
            }
            return undefined;
        }//GetSelectValue
    }
    </script>
</head>
<body>
    <fieldset>
        <legend>Get Radio Value</legend>
        <input name="Radio" type="radio" value="male" />Male
        <input name="Radio" type="radio" value="female" />Female
        <input name="GetRadioValue" type="button" οnclick="alert(JS.GetRadioValue('Radio'));"
            value="Get radio value" />
    </fieldset>
    <fieldset>
        <legend>Get Checkbox Value</legend>
        <input name="checkbox[]" type="checkbox" value="China" />China
        <input name="checkbox[]" type="checkbox" value="America" />America
        <input name="checkbox[]" type="checkbox" value="Japanese" />Japanese
        <input name="GetCheckValue" type="button" οnclick="alert(JS.GetCheckboxValue('checkbox[]'));"
            value="Get checkbox value" />
    </fieldset>
    <fieldset>
        <legend>Get Select Value</legend>
        <select id="Select" name="Select">
            <option>Select a number</option>
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
        </select>
        <input name="GetSelectValue" type="button" οnclick="alert(JS.GetSelectValue('Select'))"
            value="Get select value" />
    </fieldset>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值