简体   繁体   English

使用jquery填充数组样式表单字段

[英]Populate array style form fields with jquery

I would like to POST a form with attributes as described here (MVC friendly array): https://stackoverflow.com/a/5003686/1246219 The reason I want to POST a form rather than using ajax is that the result is a file for download, iethere is no other reason for using a form. 我想POST一个带有这里描述的属性的表单(MVC友好阵列): https ://stackoverflow.com/a/5003686/1246219我想POST一个表单而不是使用ajax的原因是结果是一个文件下载,使用表格没有其他原因。

However I have a javascript array containing the values (integers) that I would like to submit. 但是我有一个包含我想提交的值(整数)的javascript数组。 The only ways to achieve this that I can think of are either to loop through and create input elements for each entry, or I could just populate a single form field with a comma delimited string and parse it server side. 我能想到的实现这一目标的唯一方法是循环并为每个条目创建输入元素,或者我可以用逗号分隔的字符串填充单个表单字段并解析服务器端。 Is there a better way? 有没有更好的办法?

UPDATE - If anyone is interested I have ended up with the loop approach, and happened to write it with d3 instead. 更新 - 如果有人感兴趣我已经结束了循环方法,并碰巧用d3写它。

var selected = [1,2,3];
var selectionBinding = d3.select("#myForm").selectAll("input[name='myField']").data(selected);
selectionBinding.exit().remove();
selectionBinding.enter().append("input")
                .attr("type", "hidden")
                .attr("name", "myField")
                .attr("value", function (data) { return data });
$("#myForm").submit();

My suggestion would be to create a regular Javascript Array with data and have a form with input type text . 我的建议是创建一个包含数据的regular Javascript Array ,并使用一个带有input type text的表单。 When you submit the form set the value of the input text as JSON.stringify(myArray) 提交表单时,将输入文本的值设置为JSON.stringify(myArray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM