js 提交表单数据: 附件+对象的的多集合

JavaScript08

js 提交表单数据: 附件+对象的的多集合,第1张

在我们前端进行表单提交的时候,有时候会出现这种情况:Failed to convert   java.lang.String    to java.util.List

等等。

例如:

  我后台定义一个对象:

       examPaper 包含  String userId,Float userScore, MultipartFile  examFile  用户id  ,试卷分数,试卷文件

对象外面   classPaper有: String classId  String className  List<examPaper>  examPaperList

这个时候,后台接收为  ClassPaper

如果按照平常的 form-data   提交  则应按以下方式提交:

let  fd  = new FormData()

fd.append("classId ",classId )

fd.append("className ",className )

examPaperList.forEach((item,index) ->{

     fd.append("examPaperList["+index+"].userId",item.userId)

     fd.append("examPaperList["+index+"].userScore",item.userScore)

     fd.append("examPaperList["+index+"].examFile ",item.examFile )

})

以这种方式就可以实现 多附件  一一 对应提交。以避免对象转换错误问题。

JS获取form表单里的数据并输出的方法:

document.getElementById("ddd").innerHTML = document.getElementById("xxx").value + document.getElementById("yyy").value

将表单数据获取好后进行拼接赋给某个dom节点显示出来。

js获取表单数据命令是:document.getElementById("xxx").value

输出表单数据到某个dom元素内是使用:document.getElementById("ddd").innerHTML

因此要使用js输出表单数据可以先讲表单数据整合临时存储到某个变量,在统一输出到某个dom节点内

举例:

<form id="fm" name="fm">

<input type="text" id="name" />

<input type="text" id="tel" />

<input type="button" onClick="fmResult()" />

</form>

输出表单值:<div id="d"></div>

js:

<script>

function fmResult(){

var name = document.getElementById("name").value

var tel = document.getElementById("tel").value//获取值

document.getElementById("d").innerHTML = name + tel//输出表单值

}

</script>