需求一个js 就是 选中单选框 然后点击 按钮 把选中的值 传到文本框里。。

需求一个js 就是 选中单选框 然后点击 按钮 把选中的值 传到文本框里。。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>单选</title>
</head>
<body>
<div id="radio">
  <label>
    <input type="radio" value="asp" name="me" />
    asp</label>
  <label>
    <input type="radio" value="php" name="me" />
    php</label>
  <label>
    <input type="radio" value="ajax" name="me" />
    ajax</label>
</div>
<input type="button" value="提交" id="submit" />
<input type="text" id="text" />
</body>
<script>
var radio = document.getElementById("radio").getElementsByTagName("input");
var text = document.getElementById("text");
var submit = document.getElementById("submit");
submit.onclick = function(){
  for(var i=0;i<radio.length;i++){
    if(radio[i].checked){
text.value = radio[i].value;
}
 }
}
</script>
</html>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-24
//jquery
$(function (){
    //name自己改成对应的
    $('input[name="radio"]').click(function (){
        $('input[name="text"]').val($(this).val());
    });
});
//javascript
document.getElementsByName('radio').onclick=function (){
    document.getElementsByName('text')[0].value=this.value;
}
//还有一个更直接的 比如radio的name是r text的name是t
<input type="radio" value="1" onclick="javascript:$('input[name=\"t\"]').val(this.value)">
//手打的也不知道对不对,反正就这个意思,如果错了你追问我,我再修改

//你如果是要点击按钮的话
function btnclick(){
    var r = $('input[name="radio"]:checked').val();
    $('input[name="text"]').val(r);
}
<input type="text" onclick="btnclick()">

//各种情况  手打的   采纳吧

第2个回答  2015-11-24
<!DOCTYPE html>
<html>
  <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
RunJS 演示代码
</title>
<script>
onload = function(){
var rs = document.getElementsByName("r");
for(var i = 0; i < rs.length; i++){
rs[i].onclick = function(){
txt.value = this.nextSibling.nodeValue.replace(/^\s+|\s+$/g,"");
}
}
}
</script>
  </head>
<body>
    <label>
<input type="radio" name="r" />
asp
</label>
<label>
<input type="radio"  name="r"/>
jsp
</label>
<label>
<input type="radio"  name="r"/>
php
</label>
<input type="text" id="txt" />
  </body>
</html>

追问

我是需要多一步,就是选中后点击按钮才传值,不是一选中就传到input 里。

相似回答