英文:
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 -->
$('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);
}
})
});
<!-- language: lang-css -->
form {
border: 1px solid black;
}
<!-- language: 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>
<!-- 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:
{
"name": "Andi",
"password": "pass",
"role": "teacher",
"hobby": ["Sport"],
"description": "I like programming",
"city": "Jakarta"
}
Is there a simple way to get data as JSON Object after submission like the example above?
答案1
得分: 1
不太确定这是否是你真正想要的,但你可以尝试这个
$('form').submit((e)=>{
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
$('form').submit((e)=>{
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 -->
$('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);
}
})
});
<!-- language: lang-css -->
form {
border: 1px solid black;
}
<!-- language: 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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论