简体   繁体   中英

Window.prompt() is submitting my formulary

I've got a form and inside it a button that prompts a window like this:

<form action="X">
    <!-- Some other fields -->
    <script type="text/javascript">
        window.prompt("SOME TEXT", $var);
    </script>
    <!-- Submit button of the form -->
</form>

The window's purpose is to make the user able to copy a text (which is inside the variable) pressing CTRL+C and then, close the window by hitting ENTER or ESCAPE.

My trouble is that when I close the window, doesn't matter how (clicking on "Accept", "Cancel" or pressing ENTER or ESCAPE), my form is submitted.

I appreciate so much your help,

Best.

EDIT:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="#"> <button id="copiar" style="margin-left: 65px;" onclick="copyToClipboard('Copy this')">'Button'</button> <script type="text/javascript"> function copyToClipboard(text) { window.prompt("Para copiar las etiqetas pulsa Ctrl+C, Enter", text); } $(document).ready(function() { $(document).keyup(function(e) { if(e.which == 13) { e.preventDefault(); e.stopPropagation(); return false; } }); }); </script> </form>

<button id="copiar" style="margin-left: 65px;" onclick="copyToClipboard('Copy this')">'Button'</button>

That is a submit button. It is supposed to submit the form. This has nothing to do with the prompt .

Use a plain button. Add type="button" .

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