如何使用jQuery Ajax发送具有多个输入字段值的对象

时间:2018-08-15 14:58:43

标签: javascript jquery html ajax

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

因此所有这些字段都具有相同的名称。我的问题是,如何才能将它们全部整合到一个对象中以通过ajax发送到我的php脚本?

目前,这是我的发布方式(仅适用于一位用户):

$username = $form.find( "input[name='username']" ).val(),
$pass = $form.find( "input[name='password']" ).val(),
url = $form.attr( "action" );

var posting = $.post( url, {
  username: $username,
  pass: $pass,
});

但是可能会有多个具有相同名称的输入字段,所以我的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>

我以前在产品数量方面尝试过类似的事情:

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()
    })
}

做到这一点的最佳方法是什么?在我的PHP脚本中,我需要循环对象的内容,以便每个用户名和密码都必须保持在一起。

1 个答案:

答案 0 :(得分:1)

您可以像这样简单地使用jQuery方法 .serialize()

wstring str = L"Hallo  x     y   111 2222  3333 rrr 4444   ";
wchar_t* psStr = &str[0];

var postBody = $form.serialize();

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