我正在尝试从表单获取值并将其附加到表单,然后将该值作为参数传递
脚本
$(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>
在我的脚本中,我对姓名、电话和网址的值进行了硬编码,它正在工作,但我无法附加表单中的值。 所以我正在寻找
form.append("name", "(document.getElementById('name').value");
另外,在单击按钮时,我无法传递数据,因为我已放置警报,但我的代码没有输入函数
注意:我还没有处理 url 部分
最佳答案
form.append("name", "(document.getElementById('name').value");
您正在此处附加一个字符串。只需删除“
form.append("name", document.getElementById('name').value);
此外,您可能想仔细看看匿名 functions和 data types在 JavaScript 中
关于javascript - 将值附加到表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45744645/