我试图从表单中获取值并将其附加到表单,然后将该值作为参数传递
脚本
$(document).ready(function() {
$('#buttonClick').on('click', 'button',function firstCall(){
var form = new FormData();
form.append("name", "test");
form.append("phone", "2245201905");
form.append("url", "this_url");
var settings = {
"async": true,
"crossDomain": true,
"url": "url_path",
"method": "POST",
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});});
});
HTML
<form method="post" id="formSample">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" placeholder="Fullname">
</div>
<div class="form-group">
<label for="phone">phone</label>
<input type="number" name="phone" placeholder="number">
</div>
<div id="buttonClick">
<button >Submit</button></div>
在我的脚本中,我硬编码了 name、phone 和 url 的值,它正在工作,我无法从表单中附加值。所以我正在寻找
form.append("name", "(document.getElementById('name').value");
同样在单击按钮时,我无法传递数据,因为我已发出警报,但我的代码没有进入功能
注意:我还没有处理 url 部分
原文由 raulxbox 发布,翻译遵循 CC BY-SA 4.0 许可协议
要使用实际 HTML 中的值创建 FormData,您可以将该表单作为参数传递给表单数据