当有人点击单选按钮

时间:2017-12-23 17:46:46

标签: javascript forms

当有人点击是或没有单选按钮时,我想隐藏/显示几个输入字段。但不能这样做。

这是表单字段。我想当有人点击是的div应该显示,当点击时不应该隐藏它也默认它应该被隐藏。

$(function() {
  $("input[name='prev_occ']").click(function() {
    if ($("#chkYes").is(":checked")) {
      $("#dvmilitry").show();
    } else {
      $("#dvmilitry").hide();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input name="prev_occ" type="radio" id="chkYes">
<input name="prev_occ" type="radio" id="chkNo">
<div id="dvmilitry" style="display: none">
  <input name="prev_org" type="text" class="textBoxDashed" id="prev_org" value="" size="43" maxlength="50">
</div>

1 个答案:

答案 0 :(得分:-2)

在脚本标记上添加jQuery库,它可以正常工作

    $(function () {
        $("input[name='prev_occ']").click(function () {
            if ($("#chkYes").is(":checked")) {
                $("#dvmilitry").show();
            } else {
                $("#dvmilitry").hide();
            }
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<input name="prev_occ" type="radio" id="chkYes">
<input name="prev_occ" type="radio" id="chkNo" >

<div id="dvmilitry" style="display: none">
<input name="prev_org" type="text" class="textBoxDashed" id="prev_org" value="" size="43" maxlength="50">                                
</div>