简体   繁体   中英

Javascript Form creation is auto calling submit function

I dynamically create a form, but Func() is being called on page load, instead of on click

                    var addrow_f = document.createElement("form");
                    addrow_f.setAttribute('method',"post");
                    addrow_f.setAttribute('action',Func());

                    var submitelement = document.createElement('input');
                    submitelement.setAttribute("type", "submit");
                    submitelement.setAttribute("name", "dsubmit");
                    submitelement.setAttribute("value", "Submit");
                    AddChild(addrow_f, submitelement);

                    AddChild(addrow_masterContainer, addrow_f);

The action attribute just indicates to what target the form request shall be send to.

And: Since you pass the parameter with parenthesis () it will be executed immediately. If you want to pass functions as callbacks, you have to do this without parenthesis.

If you want to react on events, you have to add event listener like click on an element. So you probably want to add an click event on the submit button.

Therefore it exists a function called addEventListener available for elements. You can use it like this: submitelement.addEventListener("click", Func);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM