我将尝试将html表单的一个部分根据复选框的状态显示/隐藏。这是我拥有的核心代码:
<script src="/js/jquery.js"></script>
<script language="JavaScript">
function toggle(className){
var $input = $(this);
if($(this).prop('checked'))
$(className).show();
else
$(className).hide();
}
</script>
<fieldset><legend>Check Here
<input type="checkbox" onclick="toggle('.myClass')" ></legend>
<span class="myClass">
<p>This is the text.</p>
</span>
</fieldset>
当您单击复选框时,该元素将被隐藏并且不会再次显示。我还使用了$(this).is(':checked')
。无论是否选中,$(this).prop('checked')
似乎都会计算为false。我最好的猜测是我错误地使用了$(this)
。我在这里错过了什么?