只使用jquery进入键盘按钮

时间:2013-04-25 15:24:34

标签: javascript html css css3

hi2all我只需处理输入按钮事件,但即使我点击

,我的代码句柄也会输入

带有它的另一个按钮

例如当我点击Shift +从kb输入它处理点击我想防止这样的事情

我想处理单独输入

{的 _ __ _ __ _ __ _ __ 另一个问题 我想在每个附加文本后添加新行

我的代码在这里

<!DOCTYPE HTML>
<html>

    <head>
        <meta http-equiv="content-type" content="text/html" />
        <meta name="author" content="gencyolcu" />
        <title>Untitled 1</title>
        <style>
</style>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                $("#button1").click(function() {
                    $(this).attr("disabled", true);
                });
                $("#entr").keypress(function(e) {
                    if (e.which == 13) {
                        alert("enter pressed");
                        $("#texts").append($("#entr").val());
                    }
                });
            });
        </script>
    </head>

    <body>
        <div id="m" style="background-color: darkorange;">click me to change webpage background</div>
        <input id="entr" type="text" value="enter some text here" />
        <input id="button1" type="button" value="me" />
        <p id="texts">text in text box will appended here
            <br />
        </p>
    </body>

</html>
<script type="text/javascript">
    //change the color of webpage when clicking the div
    var x = document.getElementById("m");
    x.addEventListener("click", function() {
        document.body.style.backgroundColor = "darkturquoise";
    });
</script>

2 个答案:

答案 0 :(得分:0)

这是你需要做的。将您的活动keypress绑定到正文。

jQuery(document).ready(function(){
    jQuery('body').on('keypress', function(e){
        //  We are looking for the action "enter"
        if(e.which == 13){
            //  Must not be with shift
            if(event.shiftKey !== true){
                alert(' Enter ! ');
            }
        }
    });
});

要添加新行,您必须附加MyVar = MyVar + '<br />'+"\r\n";

答案 1 :(得分:0)

// //will work for shift+enter if (event.keyCode == 13 && event.shiftKey) { } How do I detect "shift+enter" and generate a new line in Textarea?

相关问题