如何在提交后在同一页面中显示html表单值?

时间:2017-09-30 16:25:47

标签: javascript jquery html ajax

可以给我一个例子,在提交按钮后从输入中提取数据并在同一页面中发送输出h2吗?

这是我的输入和提交按钮。

<div class="input-field col s6">
  <i class="material-icons prefix">perm_identity</i>
  <input placeholder="..." id="nameClient" name="nameClient" type="text" class="validate">
  <label for="nameClient">Nome do cliente:</label>
</div>

<button class="btn waves-effect waves-light indigo" id="publish-sell" type="submit"><i class="material-icons left">attach_money</i>EFETUAR VENDA</button>

使用此示例通过单击将类型化数据输入提取到另一个输入。

$("#copyMapResearch").click(function () {
  var endereco = $("#addressDeliveryClient").val();
  $("#pac-input").val(endereco);
});

$("#copyToForm").click(function () {
  var endereco = $("#pac-input").val();
  $("#addressProvider").val(endereco);
});

此代码的目的是将其引入ajax的complete function

$(document).ready(function () {
  $('#draft-sell').click(function () {
    var payload = {
      nameClient: $('#nameClient').val(),
      nameFantasySell: $('#nameFantasySell').val(),
      addresOfClientSell: $('#addresOfClientSell').val(),
      annotations: $('#annotations').val(),
      neighborhood: $('#neighborhood').val(),
      cep: $('#cep').val(),
      phoneLandline: $('#phoneLandline').val(),
      cellphone: $('#cellphone').val(),
      autocompleteBusinessReseller: $('#autocompleteBusinessReseller').val(),
      amountProduct: $('#amountProduct').val(),
      productSoldSell: $('#productSoldSell').val(),
      producFinalPrice: $('#producFinalPrice').val(),
      registeredDaySell: $('#registeredDaySell').val()
    };
    $.ajax({
      url: "/product/sell-draft",
      type: "POST",
      contentType: "application/json",
      processData: false,
      data: JSON.stringify(payload),
      complete: function (data) {
        // here is the place of the code that will extract the data from the data entered in the input and write in front-end
      }
    });
  });
});

这是我最终的html标签,用于显示结果。

<h2 class="left-align white-text person-name" id="nameClientReciept">

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

如果您想使用带有提交按钮的表单,则需要使用表单的submit操作,并阻止默认设置。

&#13;
&#13;
let form       = document.querySelector('form');
let nameClinet = document.querySelector('#nameClient');
let output     = document.querySelector('#output');

form.onsubmit = function(e){
  e.preventDefault(); // stop the submit from actually happening
  console.log("Client Name:", nameClient.value);  
  output.innerText = nameClient.value;
}
&#13;
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
<form>
  <div class="input-field col s6">
    <i class="material-icons prefix">perm_identity</i>
    <input placeholder="..." id="nameClient" name="nameClient" type="text" class="validate">
    <label for="nameClient">Nome do cliente:</label>
    <h2 id="output"></h2>
  </div>

  <button 
    class="btn waves-effect waves-light indigo" 
    id="publish-sell" 
    type="submit">
      <i class="material-icons left">attach_money</i>EFETUAR VENDA
    </button>
</form>
&#13;
&#13;
&#13;