通过onsubmit表单发送数组

时间:2016-01-26 14:53:22

标签: javascript php

我有一个像这样的html表单:

<form name="form" id="form" method="get" action="www.example.com" enctype="multipart/form-data" onsubmit="window.location.href = 'www.example.com/goods:' + document.getElementsByClassName('goods').serialize;  return false;" autocomplete="off">
    <input name="goods" type="text" id="<?=$goods['id']?>" class="goods" value="" />
    <input name="goods" type="text" id="<?=$goods['id']?>" class="goods" value="" />
    <input name="goods" type="text" id="<?=$goods['id']?>" class="goods" value="" />
    <input name="goods" type="text" id="<?=$goods['id']?>" class="goods" value="" />
    <input name="goods" type="text" id="<?=$goods['id']?>" class="goods" value="" />

    <input name="submit" type="submit" class="submit" value="submit" />
</form>

我试图通过onsubmit形式传递一个带有输入字段值的数组,然后重定向到上面的url,但我无法设法做到这一点。有没有办法实现它?

2 个答案:

答案 0 :(得分:4)

要在HTML表单中定义数组,您可以使用[],如下所示:

<input name="goods[]"/>

答案 1 :(得分:3)

我认为您正在尝试这样编码。将名称作为数组

    <form name="form" id="form" method="get" action="www.example.com" enctype="multipart/form-data" onsubmit="window.location.href = 'www.example.com/goods:' + document.getElementsByClassName('goods').serialize;  return false;" autocomplete="off">
        <?php foreach ($goods as $key => $good) { ?>
          <input name="goods[]" type="text" id="<?php echo $good['id']; ?>" class="goods" value="" />
        <?php } ?>
        <input name="submit" type="submit" class="submit" value="submit" />
    </form>