javascript - 将值附加到表单

标签 javascript jquery forms

我正在尝试从表单获取值并将其附加到表单,然后将该值作为参数传递

脚本

$(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);

此外,您可能想仔细看看匿名 functionsdata types在 JavaScript 中

关于javascript - 将值附加到表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45744645/

相关文章:

javascript - 具有多色段的折线图

javascript - 通过冒泡处理许多元素的点击事件

javascript - jQuery 绑定(bind)对象上的所有事件

javascript - 计算从绿色到红色的颜色值

javascript - lodash 函数不返回单个对象

javascript - 如何在javascript中将字节数组转换为图像

json - Extjs 4 : Render store data into Ext. form.Panel不使用MVC框架

JavaScript 电子邮件验证 - @ 之前的字符串变量

php - $_SERVER ['PATH_INFO' ] ..未定义索引 : PATH_INFO

javascript - 如何在嵌套的 Json 中使用 angularjs 进行过滤