日期:2014-05-17  浏览次数:20619 次

怎么把radio里被选择的值传到javascript里面?
HTML code
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
    function che() {
     alert();
      }

    </script>
</head>
<body>
<form name="rad" method="post" action="#">
<div><p><input type="radio" value="1" name="r"/>1</p>
<p><input type="radio" value="2" name="r"/>2</p>
</div>



<input type="button" value="check" onclick="che()"/>
</form>
</body>
</html>



我想让按键被点击之后, 显示出我所选择的radio的value

------解决方案--------------------
HTML code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
    function che() {
        var rs = document.getElementsByName('r');
        for (var i = 0, len = rs.length; i < len; i++) {
            if (rs[i].checked) {
                alert(rs[i].value);
                break;
            }
        }
    }
    </script>
</head>
<body>
<form name="rad" method="post" action="#">
<div><p><input type="radio" value="1" name="r"/>1</p>
<p><input type="radio" value="2" name="r"/>2</p>
</div>



<input type="button" value="check" onclick="che()"/>
</form>
</body>
</html>

------解决方案--------------------
jquery代码
<script language="javascript">
$("input[type=radio]").click(function(){
alert($(this).val());
});
</script>
------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title> New Document </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function che() {
var list = document.forms["rad"].elements("r");
for(var i=0;i<list.length;i++){
var radio = list[i];
if(radio.checked){
alert(radio.value);
}
}
}
</script>
</head>

<body>
<form name="rad" method="post" action="#">
<div>
<p><input type="radio" value="1" name="r"/>1</p>
<p><input type="radio" value="2" name="r"/>2</p>
</div>

<input type="button" value="check" onclick="che()"/>
</form>
</body>
</html>


俺是新手,不要拍我!!嘿嘿