CTRL + S提交表单和所有输入

Joh*_*lia 12 forms jquery

我有一个表单,我在我的CMS中使用,我想添加额外打开以保存表单上的按键:"Ctrl + S"

这适用于除提交按钮之外的所有输入都没有被发送,这个简单的例子显示了我的意思:

<?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>
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 10

如果submit按钮的值submit是单击的按钮,则按钮的值仅包含在请求中.

因为您直接提交表单(使用JS),所以您没有点击提交按钮,因此没有提交.

不要.submit()在表单上调用,而是尝试调用.click()要包含的提交按钮.

$(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;
});
Run Code Online (Sandbox Code Playgroud)