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

标签 javascript jquery scala playframework-2.0

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

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/

相关文章:

Scala 不确定性/缓存类?

javascript - 如何在提交前添加确认框

javascript - Discord 机器人仅对少数命令使用react

JavaScript SRC PHP 文件

javascript - 如何在ajax请求中将进度条停止在100%?

jquery - 所选文本的宽度 - jquery

scala - Scala 是否在 JamVM 上运行

java - Scala 与 Java toUpperCase/toLowerCase

javascript - 如何通过Javascript在MongoDB 'where'查询中使用外部变量?

jquery - Flexslider 标题水平折叠