冷聚变 - 验证是否选中了单选按钮

Coldfusion - Validate If Radio Button is Checked

本文关键字:单选按钮 是否 验证 冷聚变      更新时间:2023-09-26

我的单选按钮有问题。我有 2 个单选按钮,单击时我希望每个单选按钮显示和隐藏不同的文本字段。

每个单选按钮都有自己的 ID 和名称。

网页代码:

<td>
  <div class="btn-group" data-toggle="buttons">
    <label class="btn btn-default">
     <input type="radio" id="has_sub_component_1" name="has_sub_component" value="1"/>Yes
    </label>
    <label class="btn btn-default">
     <input type="radio" id="has_sub_component_0" name="has_sub_component" value="0"/>No
    </label>
 </div>
</td>

<cfif isDefined("has_sub_component_1")>
  <tr>
    <td></td>
    <td><input type="text" name="mark" id="sub_mark" value="" />
  </tr>
</cfif>
<cfif isDefined("has_sub_component_0")>
  <tr>
    <td></td>
    <td><input type="text" name="topic" id="sub_topic" value="" />
  </tr>
</cfif>

当我单击其中一个单选按钮时,它什么也没显示。我知道isDefined()函数只接受输入文本字段中的名称。如何在单击单选按钮时显示文本字段?

这绝对是基于JavaScript的解决方案的问题。

下面是一个示例:

<script type="text/javascript">
function yesnoCheck() {
    if (document.getElementById('yesCheck').checked) {
        document.getElementById('ifYes').style.display = 'block';
    }
    else document.getElementById('ifYes').style.display = 'none';
}
</script>
Yes <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="yesCheck"> No <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="noCheck"><br>
    <div id="ifYes" style="display:none">
        If yes, explain: <input type='text' id='yes' name='yes'><br>
        What can we do to accommodate you?  <input type='text' id='acc' name='acc'>
    </div>
        other 3<input type='text' id='other3' name='other3'><br>
        other 4<input type='text' id='other4' name='other4'><br>

从这个jsfiddle中抓取:

http://jsfiddle.net/QAaHP/16/

您可以使用它按类或 ID 定位任何页面元素,然后应用效果。