简体   繁体   中英

Can I prevent/avoid the alert when reloading a form in my page?

I have a simple form in my page as shown in the pic.

Is there a way to avoid the alert??. I find it annoying and in my case I don't lose any important info if I reload the form.

您确定要重新加载页面吗?

<form name="theForm" method="get"> <!-- Changed from "post" to "get" but alert keeps showing -->
Start Point     ( 
<input type="text" name="pt0x" size="4" onkeydown="keyDownPt(event, document.theForm.pt0x.value, 0, 'x')"/>,
<input type="text" name="pt0y" size="4" onkeydown="keyDownPt(event, document.theForm.pt0y.value, 0, 'y')"/> ); </br>
...
</form>

I don't have a "send" button because I update my thing onkeydown event. Could this be the reason?

Use GET as method on the form tag instead of POST.

Since POST is meant for "uploading" data to the server, the browser will rightfully warn the user about reposting the the form, but if you simply GET the data the browser assumes it's a safe action to repeat.

EDIT : I misread the dialog in the question. It's not about reposting, but about discarding form data... Try using GET instead of POST anyway (if you're not already doing it), see if it has any effect.

EDIT2 : I would be surprised if it's any simple way of disabling it using javascript (the user could probably disabled in the browser, though), since it's a reminder that you're about to discard data. If the user wants to reload the page, the browser warns that the form will be discarded but the user can dismiss it.

This should not be an issue anyway. If you need to reload the application for actual production use for some reason, try to rewrite the program to work without reloading (as this may cause the browser to re-download the code). Eg if you want to reload just to reset the form, just add a reset button.

Since you are not actually doing anything with it, you could get rid of the form entirely.

EDIT for completeness:Not using a form tag is perfectly valid HTML and validates as HTML4 strict, HTML5 and XHTML transitional. The W3C validator is here .

I think I would try disabling the submit button, since you don't intend to post the data in the first place.

The easy (and ugly) way to do this is by adding a false return statemen on your form's onsubmit handler, like so:

<form onsubmit="return false;">

Good-luck!

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