简体   繁体   English

html和javascript同名多个输入 - 如何?

[英]html and javascript same name for multiple inputs - how to?

I have never done anything like this, and I would like to know how to do it. 我从未做过这样的事情,我想知道怎么做。 I need to put 4 inputs into a sub array if it is field out 如果字段输出,我需要将4个输入放入子数组中

I know that when i $_POST the form to the server it sends the names of the inputs but how do I get the input to be allowed to have the same name 我知道当我将表单发送到服务器时,它会发送输入的名称,但是如何让输入被允许具有相同的名称

for example 例如

the sub array i need it to be in is offers here is what i dont know. 我需要它的子数组是这里提供的是我不知道的。 How do i get the following inputs 我如何获得以下输入

<input name="offers[]['minspend']" value="15.00"/>
<input name="offers[]['minspend']" value="5.00"/>

<input name="offers[]['minspend']" value="19.00"/>
<input name="offers[]['minspend']" value="8.00"/>

<input name="offers[]['minspend']" value="30.00"/>
<input name="offers[]['minspend']" value="7.00"/>

<input name="offers[]['minspend']" value="100.00"/>
<input name="offers[]['minspend']" value="10.00"/>

is this correct or wrong? 这是对还是错?

thanks 谢谢

It depends a bit on the back end technology that processes your request (java, php, whatnot), but from an html standpoint multiple elements with the same name will just send their value with the same parameter name. 它取决于处理您的请求(java,php,whatnot)的后端技术,但从html的角度来看,具有相同名称的多个元素将仅使用相同的参数名称发送它们的值。 You don't need any special [] syntax. 您不需要任何特殊的[]语法。

GET /mypage.html?offer=15.00&offer=5.0&offer=19.0 (etc, could be post too)

Most languages that provide built in support for html requests represent this request as a map, with a key named "offer" and a value that is an array or list containing the values submitted. 大多数为html请求提供内置支持的语言将此请求表示为一个映射,其中一个键名为“offer”,一个值是包含提交值的数组或列表。

For example http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterMap () 例如http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterMap ()

This form 这个表格

<input name="offers['minspend'][]" value="15.00"/>
<input name="offers['minspend'][]" value="5.00"/>

<input name="offers['minspend'][]" value="19.00"/>
<input name="offers['minspend'][]" value="8.00"/>

<input name="offers['minspend'][]" value="30.00"/>
<input name="offers['minspend'][]" value="7.00"/>

<input name="offers['minspend'][]" value="100.00"/>
<input name="offers['minspend'][]" value="10.00"/>

on doing var_dump($_POST) [assuming form method=post] give: 在做var_dump($_POST) [假设form method = post]给出:

array(1) {
    ["offers"] = > array(1) {
        ["\'minspend\'"] = > array(8) {
            [0] = > string(5)"15.00"
            [1] = > string(4)"5.00"
            [2] = > string(5)"19.00"
            [3] = > string(4)"8.00"
            [4] = > string(5)"30.00"
            [5] = > string(4)"7.00"
            [6] = > string(6)"100.00"
            [7] = > string(5)"10.00"
        }
    }
}

So, that is how you do it. 那么,你就是这样做的。

you can remove ' s around minspend. 你可以删除'周围minspend秒。 they aren't needed. 他们不需要。

You were almost there. 你快到了。 offers[]['minspend'] means that you get: offers[]['minspend']意味着你得到:

array(){
    array(){
        'minspend' => "15.00"
    }
    array(){
        'minspend' => "5.00"
    }
    .. and so on
}

So what is happening is, when you do something like arr[] = 1 , 1 is inserted into array arr . 所以发生的事情是,当你执行类似arr[] = 1arr 1插入到数组arr

Well first let me tell you what i understood from the question. 首先让我告诉你我从这个问题中理解了什么。 You want multiple input fields to have same name and then you want to select them all to perform some action. 您希望多个输入字段具有相同的名称,然后您想要全部选择它们以执行某些操作。 If this is the case i'd like to present a different approach - why don't you assign same class (cssclass) to all the controls you want to have same name. 如果是这种情况,我想提出一种不同的方法 - 为什么不将同一个类(cssclass)分配给想要具有相同名称的所有控件。 That way you'll be able to select them all together using document.getElementsByClassName("yourclassName") . 这样你就可以使用document.getElementsByClassName("yourclassName")一起选择它们。 which will return you an array of all the elements having class attribute as yourclassName . 这将返回一个包含class属性为yourclassName的所有元素的数组。 Or if you wanna stick to name then you can use document.getElementsByName("elementName"); 或者,如果你想坚持名字,那么你可以使用document.getElementsByName("elementName"); which returns you an array of elements having name as elementName . 它返回一个名为elementName的元素数组。 Hope its helpful. 希望它有所帮助。

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

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