jquery – CTRL S提交表单和所有输入

我有一个我在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>

解决方法

如果提交按钮是点击的提交按钮,则提交按钮的值仅包含在请求中.

因为你直接提交表单(使用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;
});

相关文章

1.第一步 设置响应头 header(&#39;Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...