简体   繁体   English

如何使用 JavaScript 在“s:textarea”中设置“s:radio”按钮的选定值?

[英]How to set selected value of 's:radio' button in 's:textarea' with JavaScript?

I need to set 's:textarea' value which is selected in 's:radio' button.我需要设置在 's:radio' 按钮中选择的 's:textarea' 值。

I am currently using the code below to do it.我目前正在使用下面的代码来做到这一点。 But it always set the 'In test' value in 's:textarea' which is the first value in 's:radio' button list.但它总是在 's:textarea' 中设置 'In test' 值,这是 's:radio' 按钮列表中的第一个值。

function handleCommentChange()
{
    var comment = document.getElementById('comment');

    var radioComment1 = document.getElementById('radio1');

    comment.value = radioComment1.value;

}


<td>
  <s:radio id="radio1" name="radio1" list="#{'In test':'In test','Defect':'Defect','Pass':'Pass', 'Fail':'Fail' }"
     onclick="javascript:handleCommentChange();" ></s:radio>
  <br></td>

<td><s:textarea id="comment" name="comment" cols="25" rows="5"></s:textarea></td>

Please tell how I can set the selected value of 's:radio' button in 's:textarea'.请告诉我如何在“s:textarea”中设置“s:radio”按钮的选定值。

Pass a reference to the clicked radio in to your handleCommentChange() function.将单击的收音机的引用传递给您的 handleCommentChange() function。 You can pass this , which will give the function a reference to the radio element and let you access all of its properties, etc., or you can pass this.value directly and have the function assign that string to your textarea.您可以传递this ,这将为 function 提供对 radio 元素的引用并让您访问其所有属性等,或者您可以直接传递this.value并让 function 将该字符串分配给您的文本区域。 I prefer to pass this to allow for future expansion needing access to other properties:我更喜欢通过this以允许将来需要访问其他属性的扩展:

function handleCommentChange(aRadio) {
   document.getElementById('comment').value = aRadio.value;
}


<s:radio ... onclick="handleCommentChange(this);">

The other way to do it is to setup a loop to go through each radio button and check which one has its checked property set to true.另一种方法是通过每个单选按钮设置到 go 的循环,并检查哪个单选按钮的已选中属性设置为 true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取所选单选按钮的值? - How to get the selected radio button’s value? 如何在Microsoft Edge中获取选定的单选按钮的值? - How to get selected radio button's value in Microsoft edge? 流星:如何根据数据库中的值设置单选按钮的值 - Meteor: How to set a radio button’s value depending on the value in the Database 如何将单选按钮的选定值复制到另一个单选按钮-jQuery - How to copy a radio button's selected value to another radio button - jquery 使用JavaScript检查单选按钮的值 - Using javascript to check a radio button by it's value 如何相对于选中的单选按钮更新textarea的文本? - How to update a textarea's text relative to a checked radio button? 为 Javascript 中的任何字段设置值的最佳方法,而无需事先知道它是否将成为输入、select、textarea、radio 或 checkbox 字段 - Best way to set a value to any field in Javascript without knowing beforehand if it's going to be a input, select, textarea, radio, or checkbox field 如何获得textarea的价值并使用Javascript将其放入另一个textarea? - How to get a textarea's value and put it into another textarea using Javascript? 如何使用 JavaScript - Salesforce 中的 controller 动态设置选定的单选按钮 - How to dynamically set the selected radio button with the controller in JavaScript - Salesforce 如何将所选单选按钮的文本存储在localStorage中? - How can I store a selected radio button's text in localStorage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM