CTRL + S提交表单和所有输入 [英] CTRL + S to submit form and all inputs

查看:122
本文介绍了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;
背景:#f4f4f4;
字体:普通12px / 18px Verdana,sans-serif;
身高:100%;
}
#container {
width:760px;
margin:0 auto;
padding:10px 60px;
border:solid 1px #cbcbcb;
背景:#fafafa;
-moz-box-shadow:0px 0px 10px #cbcbcb;
-webkit-box-shadow:0px 0px 10px #cbcbcb;

最小高度:100%;
height:auto!important;
身高:100%;
}

< / style>

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

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

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

< body>
< div id =container>

< form action =method =post>
< label for =>名称< / label>
< input type =text =name =namevalue =/>

< input name =savetype =submitvalue =Save/>
< input name =createtype =submitvalue =Create/>

< / form>

< / div>
< / body>
< / html>


解决方案

按钮只包含在请求中,如果它是点击 submit 按钮的话。



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



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


$ b $

  $(window).keypress(function(event){
if(!(event.which == 115 && event.ctrlKey)&&!(event.which == 19))return true;
$(#container form input [name = save])。click();
event.preventDefault();
返回false;
});


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>

解决方案

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;
});

这篇关于CTRL + S提交表单和所有输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆