日期:2014-05-16 浏览次数:20428 次
<!DOCTYPE html> <html> <head> </head> <body> <form id="myform" name="myform"> <label> <input type="radio" name="radio-test" value="test1" /> <span>Test1</span> </label> <label> <input type="radio" name="radio-test" value="test2" /> <span>Test2</span> </label> <input name="submit" type="submit" value="submit" /> </form> <script> function getElementsByName(container, name){ var result = []; var childrens = container.getElementsByTagName('*'); for(var i = 0; i < childrens.length; i++){ if(childrens[i].name === name){ result.push(childrens[i]); } } return result; } document.myform.onsubmit = function(){ var radios = getElementsByName(this, "radio-test"); for(var i = 0; i < radios.length; i++){ if(radios[i].checked){ break; } if(i === radios.length - 1){ alert("没有选择"); } } } </script> </body> </html>
------解决方案--------------------