133

javascript と JQuery を使用して、POST を使用して HTTP フォームから送信されるフィールドを追加する方法はありますか?

つまり:

<form action="somewhere" method="POST" id="form">
  <input type="submit" name="submit" value="Send" />
</form>

<script type="text/javascript">
  $("#form").submit( function(eventObj) {
    // I want to add a field "field" with value "value" here
    // to the POST data

    return true;
  });
</script>
4

7 に答える 7

187

はい。いくつかの非表示のパラメーターを試すことができます。

  $("#form").submit( function(eventObj) {
      $("<input />").attr("type", "hidden")
          .attr("name", "something")
          .attr("value", "something")
          .appendTo("#form");
      return true;
  });
于 2013-07-23T11:36:48.647 に答える
55

これを試して:

$('#form').submit(function(eventObj) {
    $(this).append('<input type="hidden" name="field_name" value="value" /> ');
    return true;
});
于 2013-07-23T11:37:32.903 に答える
11

hidden input送信する必要のある任意の値を追加できます。

$('#form').submit(function(eventObj) {
    $(this).append('<input type="hidden" name="someName" value="someValue">');
    return true;
});
于 2013-07-23T11:37:35.553 に答える
8

これは機能します:

var form = $(this).closest('form');

form = form.serializeArray();

form = form.concat([
    {name: "customer_id", value: window.username},
    {name: "post_action", value: "Update Information"}
]);

$.post('/change-user-details', form, function(d) {
    if (d.error) {
        alert("There was a problem updating your user details")
    } 
});
于 2015-09-23T21:08:40.800 に答える