简体   繁体   中英

Can a form have more than one onsubmit()?

I have a form that posts to the page it os on and I would like to use some javascript that will make the page maintain it's scroll position when the form submits. The script that I have uses onsubmit but I already have another script using that.

Is it possible to use onsubmit() for more than one script eg..

onsubmit="return validateForm(), return saveScroll()"

Thanks

或者你可以做到

onsubmit="validateForm();saveScroll(); return false;"

You can simply call both:

onsubmit="return validateForm() && saveScroll()"

Using && the form will only submit if both functions return true. You can use other logical operators to get the desired return value.

You can call any other functions you want from the first function

Eg. call saveScroll() from validateForm()

Or you could simply seperate the calls by a ; and then both will be executed.

A form can have only one onsubmit attribute.

The value of that attribute is the body of a function, so if you return from a statement in it, then you will end the function instead of continuing to the next statement.

Thus, to run both functions with an onsubmit attribute:

onsubmit="validateForm(); return saveScroll()"

You might want to change the logic there depending on how you want to react to the function calls. If the return value of validateForm matters, then you should do something with it.

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