我一直在用户选中复选框的部分上寻求帮助,该复选框可以启用某些字段,例如JavaScript中的DateTime。
例如,默认情况下,dateTime被禁用,直到用户选中Checkbox,然后DateTime才被启用。
这类似于我一直在寻找的内容,当用户选中复选框时,该内容用于显示文本。
function myFunction() {
// Get the checkbox
var checkBox = document.getElementById("myCheck");
// Get the output text
var text = document.getElementById("text");
// If the checkbox is checked, display the output text
if (checkBox.checked == true){
text.style.display = "block";
} else {
text.style.display = "none";
}
}
Checkbox: <input type="checkbox" id="myCheck" onclick="myFunction()">
<p id="text" style="display:none">Checkbox is CHECKED!</p>
感谢进阶。
答案 0 :(得分:0)
您可以通过执行以下操作来实现。我已在代码中添加了文档,以提供有关此解决方案的工作原理的解释:
var yourCheckbox = document.querySelector('#myCheck');
var yourDateField = document.querySelector('#yourDateField');
// This function will update the date field's enabled/disabled
// attribute, depending on if the yourCheckbox is checked
function updateYourDateField() {
if(yourCheckbox.checked) {
yourDateField.disabled = true;
}
else {
yourDateField.disabled = false;
}
}
// Add an event listener to the change event, that causes
// the date field to be enabled/disabled when ever the checkbox
// is clicked and the value changes
yourCheckbox.addEventListener('change', function() {
updateYourDateField();
})
// Call this to ensure your date field is in correct state
// when the script is first run
updateYourDateField();
<form>
<div>
<label>Disable/Enable control</label>
<input id="myCheck" type="checkbox" />
</div>
<div>
<label>The date field</label>
<input id="yourDateField" type="date" />
</div>
</form>