gpt4 book ai didi

javascript - scala play框架2.0.4中未选中的复选框无法设置为由Jquery或Javascript检查?

转载 作者:行者123 更新时间:2023-11-28 20:32:51 25 4
gpt4 key购买 nike

根据用户的选择取消选中框是合理的,但是未选中的框总是会导致服务器端出现表单错误,

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 )
}
}
)

然后我想出了一个解决方法,只需将附加的输入文本框更改为复选框,每当单击复选框时,我都会翻转输入文本框中的值,并将复选框设置为始终为 true,以便服务器不会提示。

那么问题是,无论我根据答案 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") ;
}

感谢您的任何提示!!!

最佳答案

Then I thought up a workaround that just change an attached input text box to checkbox, whenever checkbox is clicked,

如果您的意思是更改 type <input> 的属性元素创建并插入文档后,请注意,如果您必须支持它们,则该元素在 IE 6/7/8 中不起作用。您可能想要创建两者并根据需要隐藏/删除。

$("#fs").attr('checked', 'checked'); // prop does not work too..

你尝试过这个吗?

$("#fs").prop('checked', true);

要切换复选框,

  var curO = $("#fs").is(":checked");
if(!curO) {
$(this).attr("checked", !curO);
}

该代码块可以替换为:

this.checked = !this.checked;

尽管如果您想使用 jQuery(此处不需要),您可以使用:

$(this).prop("checked", !this.checked);

关于javascript - scala play框架2.0.4中未选中的复选框无法设置为由Jquery或Javascript检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16009437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com