在jQuery中,在表单提交后获取数据作为对象。

huangapple go评论81阅读模式
英文:

Get data as object after form submit in jQuery

问题

以下是您的代码的翻译部分:

我正在构建一个网站可以在提交后发送自定义数据代码如下

<!-- 开始代码片段js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

$('form').submit((e)=>{
  e.preventDefault();
  let data = $('form').serializeArray();
  console.log(data);
  $.ajax({
    url: 'path/to/submit',
    type: 'POST',
    data: data,
    success: (res) => {
      console.log(res);
    }
  })
});

<!-- 语言: lang-css -->

form {
  border: 1px solid black;
}

<!-- 语言: lang-html -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  Name
  <input type="text" name="name" value="Andi" /><br />
  Password
  <input type="password" name="password" value="pass" /><br />
  Role <br/ >
  <input type="radio" name="role" value="teacher" checked /> Teacher <br />
  <input type="radio" name="role" value="student" /> Student <br />
  Hobby <br/ >
  <input type="checkbox" name="hobby" value="Sport" checked /> Sport <br />
  <input type="checkbox" name="hobby" value="Music" /> Music <br />
  Description
  <textarea name="description">I like programming</textarea><br />
  City
  <select name="city">
    <option value="Jakarta">Jakarta</option>
    <option value="Bali">Bali</option>
  </select><br />
  <input type="submit" />
</form>

<!-- 结束代码片段 -->

希望这有助于您理解代码的翻译部分。如果您需要更多帮助,请随时告诉我。

英文:

I am building a website that can send custom data after submission with the below code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;form&#39;).submit((e)=&gt;{
e.preventDefault();
let data = $(&#39;form&#39;).serializeArray();
console.log(data);
$.ajax({
url: &#39;path/to/submit&#39;,
type: &#39;POST&#39;,
data: data,
success: (res) =&gt; {
console.log(res);
}
})
});

<!-- language: lang-css -->

form {
border: 1px solid black;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;form&gt;
Name
&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;Andi&quot; /&gt;&lt;br /&gt;
Password
&lt;input type=&quot;password&quot; name=&quot;password&quot; value=&quot;pass&quot; /&gt;&lt;br /&gt;
Role &lt;br/ &gt;
&lt;input type=&quot;radio&quot; name=&quot;role&quot; value=&quot;teacher&quot; checked /&gt; Teacher &lt;br /&gt;
&lt;input type=&quot;radio&quot; name=&quot;role&quot; value=&quot;student&quot; /&gt; Student &lt;br /&gt;
Hobby &lt;br/ &gt;
&lt;input type=&quot;checkbox&quot; name=&quot;hobby&quot; value=&quot;Sport&quot; checked /&gt; Sport &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;hobby&quot; value=&quot;Music&quot; /&gt; Music &lt;br /&gt;
Description
&lt;textarea name=&quot;description&quot;&gt;I like programming&lt;/textarea&gt;&lt;br /&gt;
City
&lt;select name=&quot;city&quot;&gt;
&lt;option value=&quot;Jakarta&quot;&gt;Jakarta&lt;/option&gt;
&lt;option value=&quot;Bali&quot;&gt;Bali&lt;/option&gt;
&lt;/select&gt;&lt;br /&gt;
&lt;input type=&quot;submit&quot; /&gt;
&lt;/form&gt;

<!-- end snippet -->

So, to get the data after submission, I use .serializeArray() method with returning an array of object data. But the output does not identical to my expectation:

{
  &quot;name&quot;: &quot;Andi&quot;,
  &quot;password&quot;: &quot;pass&quot;,
  &quot;role&quot;: &quot;teacher&quot;,
  &quot;hobby&quot;: [&quot;Sport&quot;],
  &quot;description&quot;: &quot;I like programming&quot;,
  &quot;city&quot;: &quot;Jakarta&quot;
}

Is there a simple way to get data as JSON Object after submission like the example above?

答案1

得分: 1

不太确定这是否是你真正想要的但你可以尝试这个

$(&#39;form&#39;).submit((e)=&gt;{
  e.preventDefault();

	const data = Object.fromEntries([...new FormData(this)]);

	console.log(data);

});
英文:

Not really sure if this is what you really want but you can try this

$(&#39;form&#39;).submit((e)=&gt;{
  e.preventDefault();

	const data = Object.fromEntries([...new FormData(this)]);

	console.log(data);

});

答案2

得分: 1

你可以使用原生的FormData对象。现在,你需要遍历所有的键,因为你有更多的值(单选按钮)具有相同的名称。所以,如果有更多具有相同名称/键的值,你将得到一个数组形式的值(使用getAll()),否则你将得到纯文本值。

$('form').submit((e) => {
  e.preventDefault();
  let formdata = new FormData(e.target);
  let data = {};
  [...formdata.keys()].forEach(key => {
    let values = formdata.getAll(key);
    data[key] = (values.length > 1) ? values : values.join();
  });
  console.log(data);
  $.ajax({
    url: 'path/to/submit',
    type: 'POST',
    data: data,
    success: (res) => {
      console.log(res);
    }
  })
});
form {
  border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  Name
  <input type="text" name="name" value="Andi" /><br /> Password
  <input type="password" name="password" value="pass" /><br /> Role <br/>
  <input type="radio" name="role" value="teacher" checked /> Teacher <br />
  <input type="radio" name="role" value="student" /> Student <br /> Hobby <br />
  <input type="checkbox" name="hobby" value="Sport" checked /> Sport <br />
  <input type="checkbox" name="hobby" value="Music" /> Music <br /> Description
  <textarea name="description">I like programming</textarea><br /> City
  <select name="city">
    <option value="Jakarta">Jakarta</option>
    <option value="Bali">Bali</option>
  </select><br />
  <input type="submit" />
</form>
英文:

You can use the native FormData object. You need to loop through all the keys now that you have more values (radio buttons) with the same name. So, if there are more values with the same name/key you get the value as an array (with getAll()) otherwise you get the plain text value.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;form&#39;).submit((e) =&gt; {
e.preventDefault();
let formdata = new FormData(e.target);
let data = {};
[...formdata.keys()].forEach(key =&gt; {
let values = formdata.getAll(key);
data[key] = (values.length &gt; 1) ? values : values.join();
});
console.log(data);
$.ajax({
url: &#39;path/to/submit&#39;,
type: &#39;POST&#39;,
data: data,
success: (res) =&gt; {
console.log(res);
}
})
});

<!-- language: lang-css -->

form {
border: 1px solid black;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;form&gt;
Name
&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;Andi&quot; /&gt;&lt;br /&gt; Password
&lt;input type=&quot;password&quot; name=&quot;password&quot; value=&quot;pass&quot; /&gt;&lt;br /&gt; Role &lt;br/&gt;
&lt;input type=&quot;radio&quot; name=&quot;role&quot; value=&quot;teacher&quot; checked /&gt; Teacher &lt;br /&gt;
&lt;input type=&quot;radio&quot; name=&quot;role&quot; value=&quot;student&quot; /&gt; Student &lt;br /&gt; Hobby &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;hobby&quot; value=&quot;Sport&quot; checked /&gt; Sport &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;hobby&quot; value=&quot;Music&quot; /&gt; Music &lt;br /&gt; Description
&lt;textarea name=&quot;description&quot;&gt;I like programming&lt;/textarea&gt;&lt;br /&gt; City
&lt;select name=&quot;city&quot;&gt;
&lt;option value=&quot;Jakarta&quot;&gt;Jakarta&lt;/option&gt;
&lt;option value=&quot;Bali&quot;&gt;Bali&lt;/option&gt;
&lt;/select&gt;&lt;br /&gt;
&lt;input type=&quot;submit&quot; /&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月6日 20:37:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定