Javascript 如何使用jquery ajax发送具有多个输入字段值的对象

Javascript 如何使用jquery ajax发送具有多个输入字段值的对象,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我有一个可以添加用户的页面。有一个按钮可以动态添加用户输入字段(用户名-密码)最多25次 所有这些字段都有相同的名称。我的问题是,如何将它们集中到一个对象中,通过ajax发送到我的php脚本 目前我是这样发布的(仅针对一个用户): 但是,可能会有多个输入字段具有相同的名称,因此我的HTML可能如下所示: <div class="card-body"> <!-- <div class="form-group row"> <label for="e

我有一个可以添加用户的页面。有一个按钮可以动态添加用户输入字段(用户名-密码)最多25次

所有这些字段都有相同的名称。我的问题是,如何将它们集中到一个对象中,通过ajax发送到我的php脚本

目前我是这样发布的(仅针对一个用户):

但是,可能会有多个输入字段具有相同的名称,因此我的HTML可能如下所示:

<div class="card-body">
  <!-- <div class="form-group row">
      <label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
      <div class="col-sm-8">
          <input class="form-control" name="username[]" value="" type="text" id="example-text-input">
      </div>
  </div>
  <div class="form-group row">
      <label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
      <div class="col-sm-8">
          <input class="form-control" name="password[]" placeholder="" value='' type="text" id="example-text-input">
      </div>
  </div> -->


  <div class="form-group fieldGroup">

        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
            <div class="col-sm-8">
                <input class="form-control" name="username[]" value="" type="text">
            </div>
        </div>
        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
            <div class="col-sm-8">
                <input class="form-control" name="password[]" placeholder="" value="" type="text">
            </div>
        </div>
          <div class="input-group-addon">
              <a href="javascript:void(0)" class="btn btn-success addMore"><span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Extra gebruiker</a>
          </div>

  </div><div class="form-group fieldGroup">

        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
            <div class="col-sm-8">
                <input class="form-control" name="username[]" value="" type="text" id="example-text-input">
            </div>
        </div>
        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
            <div class="col-sm-8">
                <input class="form-control" name="password[]" placeholder="" value="" type="text" id="example-text-input">
            </div>
        </div>
          <div class="input-group-addon">
              <a href="javascript:void(0)" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Verwijder velden</a>
          </div>

  </div><div class="form-group fieldGroup">

        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
            <div class="col-sm-8">
                <input class="form-control" name="username[]" value="" type="text" id="example-text-input">
            </div>
        </div>
        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
            <div class="col-sm-8">
                <input class="form-control" name="password[]" placeholder="" value="" type="text" id="example-text-input">
            </div>
        </div>
          <div class="input-group-addon">
              <a href="javascript:void(0)" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Verwijder velden</a>
          </div>

  </div><div class="form-group fieldGroup">

        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
            <div class="col-sm-8">
                <input class="form-control" name="username[]" value="" type="text" id="example-text-input">
            </div>
        </div>
        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
            <div class="col-sm-8">
                <input class="form-control" name="password[]" placeholder="" value="" type="text" id="example-text-input">
            </div>
        </div>
          <div class="input-group-addon">
              <a href="javascript:void(0)" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Verwijder velden</a>
          </div>

  </div><div class="form-group fieldGroup">

        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
            <div class="col-sm-8">
                <input class="form-control" name="username[]" value="" type="text" id="example-text-input">
            </div>
        </div>
        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
            <div class="col-sm-8">
                <input class="form-control" name="password[]" placeholder="" value="" type="text" id="example-text-input">
            </div>
        </div>
          <div class="input-group-addon">
              <a href="javascript:void(0)" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Verwijder velden</a>
          </div>
  </div>


  <!-- copy of input fields group -->
  <div class="form-group fieldGroupCopy" style="display: none;">

        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Gebruikersnaam</label>
            <div class="col-sm-8">
                <input class="form-control" name="username[]" value="" type="text" id="example-text-input">
            </div>
        </div>
        <div class="form-group row">
            <label for="example-text-input" class="col-sm-4 col-form-label">Wachtwoord</label>
            <div class="col-sm-8">
                <input class="form-control" name="password[]" placeholder="" value="" type="text" id="example-text-input">
            </div>
        </div>
          <div class="input-group-addon">
              <a href="javascript:void(0)" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Verwijder velden</a>
          </div>

  </div>
</div>

格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德
我以前在ProductQuantity上也尝试过类似的方法:

var elements = tpj('.quantity input[name="quantity"]'),
    url = 'includes/cartoverzicht.php',
    url1 = 'includes/shoppingcart.php',
    postBody = [];
for (var i=0; i<elements.length; i++) {
    var element = tpj(elements[i]);
    postBody.push({
        product: element.attr('id'),
        quantity: element.val()
    })
}
var elements=tpj('.quantity input[name=“quantity”]'),
url='includes/cartoverzicht.php',
url1='includes/shoppingcart.php',
后体=[];

对于(var i=0;i,您可以简单地使用jQuery方法如下:

var postBody = $form.serialize();

var posting = $.post( url, postBody, function(response){
    //console.log( response );
});
console.log($('form').serialize());

格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德
格布鲁伊克斯纳姆酒店
瓦赫特伍德

谢谢!工作得很好。
var postBody = $form.serialize();

var posting = $.post( url, postBody, function(response){
    //console.log( response );
});