我的 jsf 页面中有两个布尔类型复选框,根据第一个复选框状态禁用第二个复选框,并且在第一次加载页面时,这两个复选框都未选中。当第一个复选框被选中时,我正在通过脚本更新第二个复选框禁用状态。然后我选择第二个复选框。但问题是当我提交我的页面时,只有第一个复选框状态正在更新。这是我的托管 Bean
@ManagedBean(name = "checkBoxTest")
@SessionScoped
public class CheckBoxTest {
private boolean check1;
private boolean check2;
public boolean isCheck1() {
return check1;
}
public void setCheck1(boolean check1) {
this.check1 = check1;
}
public boolean isCheck2() {
return check2;
}
public void setCheck2(boolean check2) {
this.check2 = check2;
}
public String update(){
boolean status = this.isCheck1();
boolean status2 = this.isCheck2();
return "checkboxExample.xhtml";
}
}
这是我的 jsf 页面
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>JSF tutorial</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/myFunction.js">
</script>
</h:head>
<h:body>
<h2>CheckBox Example</h2>
<h:form id="form">
<h:outputLabel value="First Check"/>
<h:selectBooleanCheckbox name="check1" id="check1" value="# {checkBoxTest.check1}" onclick="changeCheckBox2Status()"> </h:selectBooleanCheckbox>
<h:outputLabel value="Second Check"/>
<h:selectBooleanCheckbox name="check2" id="check2" value="#{checkBoxTest.check2}" disabled="#{checkBoxTest.check1?false:true}"> </h:selectBooleanCheckbox>
<h:commandButton action="#{checkBoxTest.update()}" value="Submit2">
</h:commandButton>
</h:form>
</h:body>
</html>
我的脚本如下
function changeCheckBox2Status(){
var checkBox1Status = document.getElementById("form:check1");
if(checkBox1Status.checked){
document.getElementById("form:check2").disabled=false;
}
}