我正在尝试创建一个表单,当单击名为 verify 的按钮(供用户查看)并显示提交按钮时,该表单将表单数据附加到页面底部。此提交按钮的目的是将表单数据发布到我的 php 文件。
我尝试创建如下所示的提交按钮:
var x = document.createElement("INPUT");
x.setAttribute("type", "submit");
x.setAttribute("name", "submit");
x.setAttribute("id", "submitter");
var para= document.createElement("P");
para.appendChild(x);
我认为通过在表单标签中包含脚本标签,我仍然可以访问表单数据,但是当我查看源代码时,看起来浏览器在创建此提交按钮之前正在处理表单关闭标签。所以这个提交按钮最终不会做任何事情。我的脚本标签目前一直在底部,所以不确定位置是否有问题。
或者,我尝试创建一个隐藏的提交按钮,如下所示:
然后在我的脚本中更改类型以在单击按钮时提交:
document.getElementById('submitter').type ='submit';
但这给了我一个不能设置为“null”的属性类型。我做错了什么?
最佳答案
您是否已将按钮添加到页面?
# append the content to your body or to any other element on the page
document.body.appendChild(para)?
关于发帖请求:
The purpose of this submit button is to POST the form data to my php file
了解 jQuery.post
这对您来说应该非常简单。
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
关于javascript - 如何使用 javascript 动态激活提交按钮以通过 POST 发送表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50011225/