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" onclick="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" onclick="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" onclick="alert(JS.GetSelectValue('Select'))" value="Get select value" /> </fieldset> </body> </html>