最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using HTMLJavascript display the value of a radio button in a form textbox - Stack Overflow

programmeradmin0浏览0评论

How can I display the value of a radio button in a form text box? I'd like it to change when the radio button is clicked.

Here is my radio buttons:

<input type="radio" name="testPassed" id="TestPassed" value="TestPassed">
<input type="radio" name="testFailed" id="TestFailed" value="TestFailed">

The text box:

<textarea name="description" cols="50" rows="5" id="description">Test Status: radioButtonValueHere </textarea>

Many thanks

How can I display the value of a radio button in a form text box? I'd like it to change when the radio button is clicked.

Here is my radio buttons:

<input type="radio" name="testPassed" id="TestPassed" value="TestPassed">
<input type="radio" name="testFailed" id="TestFailed" value="TestFailed">

The text box:

<textarea name="description" cols="50" rows="5" id="description">Test Status: radioButtonValueHere </textarea>

Many thanks

Share Improve this question edited Nov 23, 2009 at 14:33 Pekka 450k148 gold badges987 silver badges1.1k bronze badges asked Nov 23, 2009 at 14:25 JamieJamie 2,56510 gold badges28 silver badges31 bronze badges 3
  • The formatting of the question looks confusing. – ndim Commented Nov 23, 2009 at 14:33
  • Set name property the same, so that <input> controls can be radio buttons: name="testResults". – artdanil Commented Nov 23, 2009 at 15:02
  • I think what @artdanil was trying to say is that you should use the name attribute to group your radio buttons appropriately. Read the following tutorial for more information: echoecho./htmlforms10.htm – Topher Fangio Commented Nov 23, 2009 at 15:41
Add a ment  | 

3 Answers 3

Reset to default 2

Well, you can implement an OnClick function with your radio button like this: <input type="radio" name="sports" value="Football" onClick="doSomething();"> or you can have a function to iterate through all the radio buttons on your form and whichever is checked, will fill the textbox with your value:

<form name="myform">

<input type="text" id="txt" />

...

<script type="text/javascript">

for (i=0; i<document.myform.sports.length; i++) 

   if (document.myform.sports[i].checked==true)
   {
           t =t +document.myform.sports[i].value
   }
}

document.getElementById("txt").value=t

You can write a function that will pass the result value to the text area:

<head>
<script type="text/javascript">
<!--
function writeResult(text){
    document.myform.testResultDescription.value = text;
}
//-->
</script>
</head>

<body>
<form name="myform">
    <input type="radio" name="testResults" id="TestPassed" value="TestPassed" onclick="writeResult('Test Passed');" />
    Test Passed<br />
    <input type="radio" name="testResults" id="TestFailed" value="TestFailed" onclick="writeResult('Test Failed');" />
    Test Failed<br />
    <textarea name="testResultDescription" cols="50" rows="5" id="testResultDescription"></textarea>
</form>
</body>

With jQuery this is really simple

HTML

<div id="radiobuttons">
<--! put here radios buttons -->
</div>

$("#radiobuttons input:radio").click(function() {
    $("textbox_id").val($(this).val());
});
发布评论

评论列表(0)

  1. 暂无评论