我正在开发一个JSP应用程序,我有一个带有复选框的表单,我尝试做的是插入并更新到我的数据库“0或1”,具体取决于它是否已选中或未选中。我的代码已经是插入“1”总是插入“1”而不是“0”如果它未经检查,当我尝试更新我的数据库时,例如从“1”到“0”或从“0”到“1” “,它不起作用,我的复选框不切换。请帮帮我!
我已经有了插入和更新的方法并且它有效,因为我可以插入或更新表单的其他字段。
JSP代码:
<tr>
<td>Principal:</td>
<td colspan="2"><input type="checkbox" id="a_principal" name="a_principal" value="<%=directorio!=null?(directorio.isPrincipal()!=false?directorio.isPrincipal(): ""):""%>" />
<%=directorio.isPrincipal()%>
</td>
</tr>
Javascript代码:
$(function (){
if($('#a_principal').val()== "true"){
$("input:checkbox").prop('checked',true);
}else{
$("input:checkbox").prop('checked', false);
}
});
提前致谢!!
答案 0 :(得分:0)
如果我是你,我会尝试在你的if / else语句中显示一些东西,看看它们正在执行哪些(即它们是否正在执行)。
如果声明为
,你也可以尝试改变你 if($('#a_principal').val() === true) { ...
希望它有所帮助。
答案 1 :(得分:0)
@paulinajr我希望你的脚本只更新复选框的状态而不是值。相反,您可以通过IDE调试您的servlet / Java代码,如eclipse。如果我是对的,您将在servlet代码中收到复选框的状态,如 on 。你可以检查它
if (request.your_getterProperty().equalsIgnoreCase("on")
&& !request.your_getterProperty().isEmpty()) {
// Do the stuff
}
如果这有帮助,请告诉我。