我有这个复选框:
<div>
<input required type="checkbox" id="effectiveness" name="effectiveness">
<label for="effectiveness">Wirksamkeit</label>
</div>
这个按钮提交:
<input type="submit" id="commitChanges" name="commitChanges" value="Freigeben" tal:condition="python: not training['released'] "/>
全部在一个表格中。如果复选框的值无效,则在打开页面时它是红色的,而不仅仅是在按下按钮之后。有谁知道如何改变它?
编辑(额外信息): 我不希望复选框为红色。我希望它正常,直到我按下“Wirksamkeit”按钮。如果复选框的值无效,它应该变成红色
编辑:
这是完整的代码:
<tr tal:define="effectiveness view/get_effectiveness">
<td>
<select name="effectiveness_helper" id="effectiveness_helper" style="display: none;">
<option value="default">default</option>
<option value="yes">yes</option>
<option value="no">no</option>
</select>
<div>
<input required type="checkbox" id="effectiveness" name="effectiveness">
<label for="effectiveness">Wirksamkeit</label>
</div>
<script tal:condition="python: effectiveness['effectiveness'] == 2">
$("#effectiveness").prop("indeterminate", true).prop("checked", false)
</script>
<script tal:condition="python: effectiveness['effectiveness'] == 1">
$("#effectiveness").prop("checked", true)
</script>
<script tal:condition="python: effectiveness['effectiveness'] == 0">
$("#effectiveness").removeProp('required').prop("checked", false)
</script>
<script tal:condition="python: not training['effectiveness_verification']">
$("#effectiveness").removeProp('required');
</script>
<script tal:condition="python: training['released']">
$("#effectiveness").removeProp('required').prop("disabled", "disabled");
</script>
<script>
$("#effectiveness").change(function() {
if (!$(this).is(":checked")) {
$(this).removeProp('required');
}
});
let checkbox = document.getElementById("effectiveness");
let dropdown = document.getElementById("effectiveness_helper");
checkbox.addEventListener("change", (event) => {
let checked = event.target.checked;
dropdown.value = checked ? "yes" : "no";
});
</script>
</td>
</tr>
这是一个下拉菜单,它可以像我想要的那样工作。只有在我单击按钮后,下拉菜单才会变红freigeben
。
<tr tal:define="overall view/get_training">
<td tal:content="overall/rating" tal:condition="python: training['released']"></td>
<td tal:condition="python: not training['released']" id="rating" >
<select required size="1" id="rating" name="rating">
<option value=="">Bitte Auswählen</option>
<option tal:define="rating overall/rating;" tal:repeat="rate python:view.get_all_training_evaluations(rating)"
tal:content="rate/text" tal:attributes="selected python:rate['selected']">
</option>
</select>
</td>
</tr>
代码可能看起来有点奇怪,因为它是一个三态复选框,而第三个状态(默认)是唯一的无效状态。
我尝试使用普通的 2StateCheckbox 进行测试,它的工作方式与我预期的一样。我认为问题是,我将属性“检查”设置为假。(我认为是因为,如果我单击普通复选框并将值设置为无效(未选中),而不是将焦点设置到另一个元素,它也会变成红色)。但我仍然不知道如何解决这个问题。我已经尝试将焦点放在另一个元素上。在将 checked 属性设置为 false 之后,我还使用 jQuery 设置了属性(换句话说,首先是 $("#effectiveness").prop("checked", false); 和第二个 $("#effectiveness").prop('required ', true); 没有在 html 标签内设置所需的属性)。但没有任何效果。