我的单选按钮有问题。我有2个单选按钮,点击后我希望每个单选按钮显示和隐藏不同的文本字段。
每个单选按钮都有自己的ID和名称。
html代码:
<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()函数只接受输入文本字段中的名称。如何在单击单选按钮时显示文本字段?
答案 0 :(得分:1)
这绝对是基于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:
您可以使用它按类或ID定位任何页面元素,然后应用效果。