Window.prompt() is submitting my formulary

时间:2015-10-06 08:32:48

标签: javascript jquery html

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>

1 个答案:

答案 0 :(得分:1)

<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".