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

I have a form that posts to the page it os on and I would like to use some javascript that will make th

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745087916a4610516.html

相关推荐

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

    I have a form that posts to the page it os on and I would like to use some javascript that will make th

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信