首页 > 解决方案 > 从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>    

标签: javascripthtml

解决方案


您可以使用submit()在 JS 中提交表单。

在您的代码中,这将是:

submitForm: function() {
    document.getElementById("becomessaform").submit();
};

推荐阅读