根据用户的选择让未选中的框是合理的,但是未选中的框总是会导致服务器端的表单错误,
def config(path: String) = Action { implicit request =>
configForm.bindFromRequest.fold(
formWithErrors => { // if checkedbox is not checked, the action in the server side will match this line
BadRequest(formWithErrors.toString)
},
configs => {
. ....})}
配置表单:
val configForm = Form(
mapping(
"k" -> optional(number),
"gc" -> optional(text),
"doStateCutOff" -> optional(text),
"stateCutoff" -> number(),
"doTimeCutOff" -> optional(text),
"timeCutoff" -> number(),
"doRegex" -> optional(text),
"regex" -> nonEmptyText,
"dochecklist" -> optional(text),
"pl" -> mapping(
"fs" -> checked("File System"),
"fsh" -> nonEmptyText,
"location" -> checked("Location"),
"locationh" -> nonEmptyText,
"pic" -> checked("Picture"),
"pich" -> nonEmptyText,
"deviceid" -> checked("DeviceID"),
"deviceidh" -> nonEmptyText,
"network" -> checked("Network"),
"networkh" -> nonEmptyText
)
{ (_, fs, _, loc, _, pic, _, dev, _, ntw ) => PropertyCheckList(fs, loc, pic, dev, ntw) }
{ pl => Some(true, pl.filesystem, true, pl.location, true, pl.picture,true, pl.deviceid, true, pl.network)}
)
{
(k, gc, doStateCutOff, stateCutoff, doTimeCutoff, timeCutoff,//verbose,
doRegex, regex, dochecklist,
propertyList )
=> {
Configs(k, gc, doStateCutOff, stateCutoff, doTimeCutoff, timeCutoff,
doRegex, regex, dochecklist,
propertyList
) }
}
{
configs => {
Some(configs.k, configs.gc, configs.doStateCutOff, configs.stateCutoff, configs.doTimeCutoff, configs.timeCutoff,
configs.doRegex, configs.regex, configs.dochecklist,
configs.propertyList )
}
}
)
然后我想出了一个解决方法,只需将附加的输入文本框更改为复选框,每当单击复选框时,我就会翻转输入文本框中的值,并将复选框设置为始终为真,这样服务器就不会抱怨。
那么问题是,无论我尝试基于答案Setting "checked" for a checkbox with jQuery? 它只是不工作!
复选框的值只能由服务器端设置吗??????
在表格中:
@checkbox(
configForm("fs"),
'_id -> "fs",
'_label -> "File System",
'_showConstraints -> false
)
@inputText( configForm("fsh"),
'_id -> "fsh",
'_showConstraints -> false
)
在脚本中:
我尝试测试设置的复选框值(在服务器端初始化表单时,我将复选框初始值设置为 false):
<script>
$(document).ready(function (){
var curO = $("#fs").is(":checked");
if(!curO) {
alert(!curO) // true
$("#fs").attr('checked', 'checked'); // prop does not work too..
alert($("#fs").is(":checked")); // still false
}
并在复选框事件功能中:
$("#fs").onchange(function(){
var curO = $("#fs").is(":checked");
if(!curO) {
$(this).attr("checked", !curO);
}
var curInput = $("#fsh").val();
if(curInput == "true"){
$("#fsh").val("false") ;
}else {
$("#fsh").val("true") ;
}
感谢您的任何提示!!!