简体   繁体   English

通过 $.post() 发布输入,没有<form>目的

[英]posting inputs via $.post() with no <form> object

I need to implement form within the form - to have an option of posting to the server only 2 inputs from the form.我需要在表单中实现表单 - 可以选择将表单中的 2 个输入发布到服务器。 I thought the solution will be to send them over $.post() method but I don't know how to prepare request correctly.我认为解决方案是通过$.post()方法发送它们,但我不知道如何正确准备请求。

html: html:

<form>
  <input type="text" id="1">
  <input type="text" id="2">
  <div>
    <input type="text" id="3">
    <input type="text" id="4">
    <button type="submit" id="part_submit">
      Parially submit
    </button>
  </div>
  <input type="text" id="5">
  <button type="submit" id="submit">
    Submit
  </button>
</form>

js: js:

$("#part_submit").on("click", (e) => {
    e.preventDefault();
    let value1 = $("#3").val();
  let value2 = $("#4").val();
  let data = {"value1" : value1, "value2": value2};
  let sdata = data.serialize();
  $.post("my_server_url", sdata, function(data){
    console.log(data);
  });
})

result: TypeError: data.serialize is not a function"结果: TypeError: data.serialize is not a function"

I realize that usually if I would like to post a form I would have to do: $(form).serialize() and that would work but I cannot nest form into form.我意识到通常如果我想发布一个表单,我必须这样做: $(form).serialize()并且这会起作用,但我不能将表单嵌套到表单中。 So how to do this?那么如何做到这一点呢?

I need to implement form within the form - to have an option of posting to the server only 2 inputs from the form.我需要在表单中实现表单 - 可以选择将表单中的 2 个输入发布到服务器。 I thought the solution will be to send them over $.post() method but I don't know how to prepare request correctly.我认为解决方案是通过$.post()方法发送它们,但我不知道如何正确准备请求。

html: html:

<form>
  <input type="text" id="1">
  <input type="text" id="2">
  <div>
    <input type="text" id="3">
    <input type="text" id="4">
    <button type="submit" id="part_submit">
      Parially submit
    </button>
  </div>
  <input type="text" id="5">
  <button type="submit" id="submit">
    Submit
  </button>
</form>

js: js:

$("#part_submit").on("click", (e) => {
    e.preventDefault();
    let value1 = $("#3").val();
  let value2 = $("#4").val();
  let data = {"value1" : value1, "value2": value2};
  let sdata = data.serialize();
  $.post("my_server_url", sdata, function(data){
    console.log(data);
  });
})

result: TypeError: data.serialize is not a function"结果: TypeError: data.serialize is not a function"

I realize that usually if I would like to post a form I would have to do: $(form).serialize() and that would work but I cannot nest form into form.我意识到通常如果我想发布一个表单,我必须这样做: $(form).serialize()并且这会起作用,但我不能将表单嵌套到表单中。 So how to do this?那么如何做到这一点呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM