在scala play框架2.0.4中,Jquery或Javascript不能设置为选中复选框

unchecked checkbox can't be set to be checked by Jquery or Javascript in scala play framework 2.0.4?

本文关键字:Javascript 不能 设置 复选框 Jquery play scala 框架      更新时间:2023-09-26

根据用户的选择设置未选中框是合理的,但是未选中的框总是在服务器端导致表单错误,

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 => {
        . ....})}

configForm:

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

然后我想到了一个解决方案,只需将附加的输入文本框更改为复选框,每当单击checkbox时,我就翻转输入文本框中的值,并将checkbox设置为true,这样服务器就不会报错。

那么问题是,无论我如何尝试基于答案设置"检查"。jQuery的复选框?这就是行不通!

复选框的值是否只能由服务器端设置?????

格式为:

  @checkbox(
            configForm("fs"), 
            '_id -> "fs",
            '_label -> "File System", 
            '_showConstraints -> false
        )
   @inputText(  configForm("fsh"), 
                '_id -> "fsh",
              '_showConstraints -> false
         )

脚本中:

我试图测试设置checkbox值(当在服务器端初始化表单时,我将复选框初始值设置为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") ; 
                }

谢谢你的提示!!

然后我想到了一个解决方案,只是改变附加的输入文本复选框到复选框,当复选框被点击时,

如果你的意思是改变<input>元素的type属性后,它被创建并插入到文档中,要知道,这是不工作的ie6/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);

尝试在表单类上将fs字段声明为boolean。我不是很熟悉Scala,但我认为checked(...)是什么使字段必需的,这就是为什么你的表单提交失败,当复选框没有选中。

val configForm = Form(
  ...
  "fs" -> boolean,
  ...
}