Javascript 隐藏输入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10939840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:39:52  来源:igfitidea点击:

Javascript Hidden Input Array

javascriptarraysinputhidden

提问by szrrizvi

Is it possible to have the value of a hidden input field as an array and then have it passed to the Spring MVC controller?

是否可以将隐藏输入字段的值作为数组,然后将其传递给 Spring MVC 控制器?

function foo(){
 var myArray = new Array();
 myArray[0] = "Hello";
 myArray[1] = "World!";
 document.getElementById("hiddenInp").value=myArray;
}

And then in the controller do something like

然后在控制器中做类似的事情

@RequestMapping ...
public String test(HttpServletRequest request){
String[] myArray = request.getParameter("hiddenInp");
// Assuming that the name of the hidden input field is also hiddenInp
System.out.println(myArray[0] + myArray[1]);
...
}

How about if I am working with an associative array? Where the indices are string rather than int

如果我正在使用关联数组呢?其中索引是字符串而不是 int

回答by Sampson

Your best option would be to stringifythe array and then assign it.

您最好的选择是对数组进行字符串化,然后对其进行分配。

element.value = JSON.stringify( ["Hello", "World"] );

The resulting value will be a JSONstring that can then be parsed on the server, recreating the array. This approach works for objects as well if you wish to have something resembling an associative array.

结果值将是一个JSON字符串,然后可以在服务器上解析该字符串,重新创建数组。如果您希望拥有类似于关联数组的东西,这种方法也适用于对象。

I should note that while JSONhas fairly good support across browsers today, older browsers may not support it. You can polyfill the feature fairly easilyin those cases:

我应该注意的是,虽然JSON今天在浏览器中得到了相当好的支持,但较旧的浏览器可能不支持它。在这些情况下,您可以很容易地填充该功能

回答by Ian

However you set it in Javascript, you'll have to parse it on the server. That probably means splitting the value by "," into a C# array, then accessing it as you want. All values sent in a form submission are sent as a string exactly as-is.

无论您如何在 Javascript 中设置它,您都必须在服务器上解析它。这可能意味着将值按“,”拆分为 C# 数组,然后根据需要访问它。在表单提交中发送的所有值都按原样作为字符串发送。

You might want to use http://www.w3schools.com/jsref/jsref_join.aspto set the value of the hidden input. It's not necessary though - it seems to be fine without using .join(). But it's important to remember that the value will be a string on the server, not an array.

您可能想要使用http://www.w3schools.com/jsref/jsref_join.asp来设置隐藏输入的值。虽然没有必要 - 不使用 .join() 似乎没问题。但重要的是要记住,该值将是服务器上的字符串,而不是数组。

回答by Bergi

You can only pass a string as a parameter, as well as to an input's value. That means you Array will automatically be converted to a string by joining it with ";", you could do that manually with the .join()method, too.

您只能将字符串作为参数传递,也可以传递给输入的值。这意味着您的 Array 将通过将其与“ ;”连接来自动转换为字符串,您也可以使用.join()方法手动执行此操作。

Serverside you then will need to split that string by the chosen delimiter.

然后,服务器端您需要通过所选的分隔符拆分该字符串。

If you want to send them as an array, afaik you will need two input elements:

如果要将它们作为数组发送,afaik 您将需要两个输入元素:

<input type="hidden" name="hiddenInp[]"<!-- should be more descriptive --> value="a" />
<input type="hidden" name="hiddenInp[]"<!-- the same name --> value="b" />

回答by Randika Vishman

I found the answer in via the following link.

我通过以下链接找到了答案。

When you want to assign any JavaScript Object or an Array do it as follows:

当您想分配任何 JavaScript 对象或数组时,请执行以下操作:

var javascript_variable = 'object' or ['fruit1', 'fruit2', 'fruit3'];
$( '#hiden_input_element_unique_id' ).val( JSON.stringify(javascript_variable) );

After form submission, in the serverside we have to decode it as follows:

表单提交后,在服务器端我们要解码如下:

$fruits_array = json_decode( $_POST['hiden_input_element_unique_name'] );

This way you can get any JavaScript array in the server-side, which you set it inside to a form element via JavaScript, in the client-side!

通过这种方式,您可以在服务器端获取任何 JavaScript 数组,在客户端通过 JavaScript 将其设置为表单元素!