Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CTRL + S to submit form and all inputs

Tags:

jquery

forms

I have a form that I use in my CMS that I would like to add the extra open to save the form on keypress: "Ctrl + S"

This works for all inputs apart from the submit buttons are not being sent, this simple example shows what I mean:

<?php 
if(isset($_POST['save'])){
    die('save= ' . $_POST['save']);
}
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8" />

    <title></title>

    <style type="text/css">
        html { height: 100%; }
        body {
            color: #262626;
            background: #f4f4f4;
            font: normal 12px/18px Verdana, sans-serif;
            height: 100%;
        }
        #container {
            width: 760px;
            margin: 0 auto;
            padding: 10px 60px;
            border: solid 1px #cbcbcb;
            background: #fafafa;
            -moz-box-shadow: 0px 0px 10px #cbcbcb;
            -webkit-box-shadow: 0px 0px 10px #cbcbcb;

            min-height: 100%;
            height: auto !important;
            height: 100%;
        }

    </style>

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    (function($){
        $(document).ready(function(){   

            // Save Form
            $(window).keypress(function(event) {
                if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
                $("#container form").submit();
                event.preventDefault();
                return false;
            });

        });
    })(jQuery);
    </script>
</head>

<body>
    <div id="container">

        <form action="" method="post">
            <label for="">Name</label>
            <input type="text=" name="name" value="" />

            <input name="save" type="submit" value="Save" />
            <input name="create" type="submit" value="Create" />

        </form>

    </div>
</body>
</html>
like image 721
John Magnolia Avatar asked Aug 26 '11 13:08

John Magnolia


2 Answers

The value of a submit button is only included in the request if it is the submit button that was clicked on.

Because you're submitting the form directly (using JS), you're not clicking on a submit button, so none are been submitted.

Instead of calling .submit() on the form, try calling .click() on the submit button you want to be included.

$(window).keypress(function(event) {
    if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
    $("#container form input[name=save]").click();
    event.preventDefault();
    return false;
});
like image 51
Matt Avatar answered Oct 01 '22 07:10

Matt


Try:

if($("#container form :submit:first").length)
{
   $("#container form :submit:first").click();
}
else
{
  $("#container form").submit();
}

It will trigger a click on the first submit-button(if available)

like image 35
Dr.Molle Avatar answered Oct 01 '22 06:10

Dr.Molle