好的,我正在尝试根据复选框选择显示 2 个表单字段。这 2 个表单字段是必需的。所以基本上当复选框被选中时,我需要显示这些字段并让它们成为必需的。否则它们是隐藏的,因此不需要。我有显示隐藏排序工作。它不会在取消选择时可靠地隐藏(选择组中的另一个复选框)。此外,当它们被隐藏时,它们仍然是必需的,所以我得到了一个很好的错误......
我被困住了。任何有建议的人。我还是很新。提前致谢。这是我下面的内容:
<label for="nothing">
Test Method<br/>
<label>
<input type="checkbox" class="testmethod" id="kickback" name="kickback" value="yes" onclick="javascript:yesnoCheck();" tabindex="5" <?= ( $settings['kickback'] == 'yes' ? 'checked = "checked"' : '' ) ?>>
Kickback
</label>
<label>
<input type="checkbox" class="testmethod" id="manual" name="manual" value="yes" onclick="javascript:yesnoCheck();" tabindex="5" <?= ( $settings['manual'] == 'yes' ? 'checked = "checked"' : '' ) ?>>
Manual
</label>
<label>
<input type="checkbox" class="testmethod" id="beastmode" name="beastmode" value="yes" onclick="javascript:yesnoCheck();" tabindex="5" <?= ( $settings['beastmode'] == 'yes' ? 'checked = "checked"' : '' ) ?>>
Beast Mode
</label>
</label>
<label for="my-text-field" id="mytextfield" style="visibility:hidden">
My Text Field <em> <small>*Required*</small></em>
<input type="text" name="mytextfield" value="<?= $settings['mytextfield'] ?>" tabindex="6" required/>
</label>
<label for="my-number-field" id="mynumberfield" style="visibility:hidden">
Number of Images <em> <small>*Required*</small></em>
<input type="number" name="mynumberfield" value="<?= $settings['mynumberfield'] ?>" tabindex="7" required/>
</label>
这是我的 JS
$("input.testmethod").click(function() {
$('input:checkbox[class="' + $(this).attr('class') + '"]').prop('checked', false);
$(this).prop('checked', true);
});
function yesnoCheck() {
if (document.getElementById('beastmode').checked)
{
document.getElementById('mytextfield').style.visibility = 'visible'
document.getElementById('mynumberfield').style.visibility = 'visible';
}
else
{
document.getElementById('mytextfield').style.visibility = 'hidden';
document.getElementById('mynumberfield').style.visibility = 'hidden';
}
}