根据使用javascript选择的默认单选按钮显示Div

时间:2014-11-14 10:01:33

标签: javascript

我有一个基于两个单选按钮的表单。如果在页面加载时选择了一个单选按钮,我想显示DIV。如果值为“是”'保存在表单上然后在每个页面上加载DIV应该显示。如果表单上没有保存任何值,则DIV不应该可见。

P.S。我已经在DIV show上实现了这个功能,并且仅使用Javascript隐藏了单选按钮选择。

这是我的代码: - 页面的第一部分。

                    <script>
                    function yesnoinsCheck() {
                        if (document.getElementById('yesinsCheck').checked) {
                            document.getElementById('showexplain').style.display = 'block';
                            style.opacity = 0;
                            setTimeout(fade, 40);
                        }
                        else document.getElementById('showexplain').style.display = 'none';
                        style.opacity = 1;
                        setTimeout(fade, 40);
                    }

                </script>

<tr>
                        <td>
                            <input type="radio" class="had-insurance" onchange="javascript:yesnoinsCheck();" id="yesinsCheck" name="125" value="1" <?php if($detail_trailer['had_insurance_'] == '1') echo 'checked'?> checked=""><span class="radio-text">Yes</span>
                            <input type="radio" class="had-insurance" onchange="javascript:yesnoinsCheck();" id="noinsCheck" name="125" value="2" <?php if($detail_trailer['had_insurance_'] == '2') echo 'checked'?>><span class="radio-text">No</span>
                        </td> 
                    </tr>


                     <tr id="showexplain" style="display: none;"></tr>

页面的部分: -

<script type="text/javascript">

function yesnoCheck() {
    if (document.getElementById('yesCheck').checked) {
        document.getElementById('boxphysical').style.display = 'block';
    }
    else document.getElementById('boxphysical').style.display = 'none';

}
</script>

<td>
                            <input type="radio" class="physical-damage" onclick="javascript:yesnoCheck();" id="yesCheck" name="120" value="1" <?php if($detail_trailer['physical_damage_insurance'] == '1') echo 'checked'?> checked=""><span class="radio-text">Yes</span>
                            <input type="radio" class="physical-damage" onclick="javascript:yesnoCheck();" id="noCheck" name="120" value="2" <?php if($detail_trailer['physical_damage_insurance'] == '2') echo 'checked'?>><span class="radio-text">No</span>
                        </td> 
  <tr id="boxphysical" style="display:none;" ></tr>

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:已编辑

       <script>
      function atLoadTime(){
      if (document.getElementById('yesinsCheck').checked) {
          document.getElementById('showexplain').style.display = 'block';
      }

      if (document.getElementById('yesCheck').checked) {
          document.getElementById('boxphysical').style.display = 'block';
      }
      }
                    function yesnoinsCheck() {
                        if (document.getElementById('yesinsCheck').checked) {
                            document.getElementById('showexplain').style.display = 'block';
                           // style.opacity = 0;
                            //setTimeout(fade, 40);
                        }
                        else document.getElementById('showexplain').style.display = 'none';
                       // style.opacity = 1;
                        //setTimeout(fade, 40);
                    }


                    function yesnoCheck() {
                        if (document.getElementById('yesCheck').checked) {
                            document.getElementById('boxphysical').style.display = 'block';
                           // style.opacity = 0;
                            //setTimeout(fade, 40);
                        }
                        else document.getElementById('boxphysical').style.display = 'none';
                       // style.opacity = 1;
                        //setTimeout(fade, 40);
                    }

                </script>
<body onload="javascript:atLoadTime();">


                            <input type="radio" class="had-insurance" onchange="javascript:yesnoinsCheck();" id="yesinsCheck" name="125" value="1" <?php if($detail_trailer['had_insurance_'] == '1')echo 'checked'?> ><span class="radio-text">Yes</span>
                            <input type="radio" class="had-insurance" onchange="javascript:yesnoinsCheck();" id="noinsCheck" name="125" value="2" <?php if($detail_trailer['had_insurance_'] == '2') echo 'checked'?>><span class="radio-text">No</span>
                        <input type="radio" class="physical-damage" onclick="javascript:yesnoCheck();" id="yesCheck" name="120" value="1" <?php if($detail_trailer['physical_damage_insurance'] == '1') echo 'checked'?>><span class="radio-text">Yes</span>
                            <input type="radio" class="physical-damage" onclick="javascript:yesnoCheck();" id="noCheck" name="120" value="2" <?php if($detail_trailer['physical_damage_insurance'] == '2') echo 'checked'?>><span class="radio-text">No</span>



                     <div id="showexplain" style="display: none;">hello there</div>

                      <div id="boxphysical" style="display: none;">second div............</div>
                     </body>

这可以解决你的问题。如果你正在使用tr第一个创建表标签。