最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can a form have more than one onsubmit()? - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Jan 20, 2013 at 18:59 tatty27tatty27 1,5545 gold badges37 silver badges74 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Or you could do

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.

发布评论

评论列表(0)

  1. 暂无评论