javascript - 从javascript函数触发表单的动作属性
问题描述
我已经验证了所有表单字段,之后,我想从提交功能触发表单操作。我尝试了很多方法,但似乎没有任何效果。任何帮助表示赞赏。
(function() {
//FORM VALIDATOR
formValidator = {
init: function() {
this.cacheDom();
this.bindEvents();
},
cacheDom: function() {
//MAIN PARENT ELEMENT
this.contactForm = document.getElementById("contactForm");
//MAIN FORM ELEMENTS
this.formBody = document.getElementById("formBody");
this.inputContainer = document.getElementsByClassName("inputContainer");
//USER INPUT ELEMENTS
//INPUT FIELDS
this.fields = {
company: document.getElementById("company"),
industry: document.getElementById("industry"),
//rest of the fields
};
this.submitBtn = document.getElementById("submit");
}
submitForm: function() {
//I want to trigger form action from this part but nothing seems to work
document.getElementById("myForm").action = "https://google.com";
}
};
//INITIATE FORM VALIDATOR
formValidator.init();
}());
//HTML
<div id="formBody" class="formBody">
<form action="https://google.com" method="POST" name="becomessaform" id="becomessaform">
<input type=hidden name="oid" value="****">
<input type=hidden name="retURL" value="">
<div class="row form-fields ">
{/* all fields go here */}
</div>
<div class="row form-fields submit-button-cf"><input type="submit" id="submit" name="submit" class="button-submit"/></div>
</form>
</div>
解决方案
您可以使用submit()在 JS 中提交表单。
在您的代码中,这将是:
submitForm: function() {
document.getElementById("becomessaform").submit();
};
推荐阅读
- javascript - 第一次尝试对 Javascript 和 Node 使用 react 时出现错误,我无法在模块外使用 import 语句
- docker - 如何使用 docker 容器内的 fluent-bit 访问登录的日志
- git - 进行更改并以分离的头部状态提交它们
- java - 如何编写正在读取文件的函数的junit测试用例?
- azure-data-factory-2 - 在每月的第一个工作日触发
- javascript - useEffect 中的 useHistory 钩子在玩笑测试中创建无限循环
- python - 创建一个循环来绘制图形
- sql - varchar MS SQL server 中的平均字符数
- java - ec2重启后无法连接到java中的mongodb ec2实例
- php - PhpWord 在模板中添加页脚