当用户按下“插入”键时,如何提交表单?

How can I prompt for confirmation before submitting a form after they press the Insert key, and submit the form without confirmation when the user pushes the Enter/Return key ?

Summary of actions intended

  • INSERT : Prompt for confirmation. Submit form if confirmed.
  • ENTER : Submit form.

I have the following html form :

<form action="hhhhhhh.php" method="post" >
   <input type="text" name="DatInvNew"/>
   <input type="text" name="SumInvNew"/>
   <input type="text" name="KolInvNew"/>
   <input type="submit" value="next"/>
</form>

The following example does these things (as per the original question) :

  • INSERT : Prompt for confirmation. Submit form if confirmed.
  • ENTER : Submit form.

document.onkeypress = function (e) {
    e = e || window.event;
    if(e.keyCode == 45) {
        if(confirm("Would you like to submit this form?")) {
            document.getElementById("myForm").submit();
        }
    } else if (e.keyCode == 13) {
        document.getElementById("myForm").submit();
    }
};

And change your html slightly :

<form action="hhhhhhh.php" method="post" id="myForm">
    <input type="text" name="DatInvNew"/>
    <input type="text" name="SumInvNew"/>
    <input type="text" name="KolInvNew"/>
    <input type="submit" value="next"/>
</form>

Here is the answer..

Try it once, Just copy and past it..

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
    $('#inputform').on('keydown', 'input', function (event) {
          switch(event.which) {
            case 13: // enter
        event.preventDefault();
        var $this = $(event.target);
        var index = parseFloat($this.attr('data-index'));
        $('[data-index="' + (index + 1).toString() + '"]').focus();
                break;

            case 45: // insert
        $('#click-to-submit').trigger('click');
        break;
          }
    });
});

</script>
<form id='inputform' action="hhhhhhh.php" method="post">
    <input type="text" name="DatInvNew" data-index="1" />
    <input type="text" name="SumInvNew" data-index="2" />
    <input type="text" name="KolInvNew" data-index="3" />
    <input type="submit" value="next" class="click-to-submit" id="click-to-submit" />
</form>