简体   繁体   English

按下Enter键后,隐藏的提交按钮将不会以chrome提交

[英]Hidden submit button wont submit in chrome when enter is pressed

I have a problem with a hidden submit button. 隐藏的提交按钮有问题。 I have an input field I use for an Ajax(jQuery) chat, people should enter text and press enter to submit. 我有一个用于Ajax(jQuery)聊天的输入字段,人们应该输入文本,然后按Enter提交。 This works fine in Firefox but not in chrome, I think it treats it as if it's not there. 这在Firefox中可以正常工作,但在chrome中则不能,我认为它似乎不存在。

I'm using the following code: 我正在使用以下代码:

$('#CommentChatForm').submit(function(event){ 
    ...
}

And I use this to hide the submit button: 我用它来隐藏提交按钮:

$('#CommentChatForm input:submit').hide();

Which makes the HTML look like this: 这使得HTML看起来像这样:

<div class="submit">
    <input type="submit" value="Submit" style="display: none;">
</div>

I've also tried using keypress() by adding the following on top: 我还尝试通过在顶部添加以下内容来使用keypress():

$('#CommentMessage').keypress(function(e){
    if(e.which == 13){
        $('#CommentChatForm').submit();
    }
})

This makes it work fine in Chrome but in Firefox it causes it to submit twice instead of once. 这样可以使其在Chrome浏览器中正常工作,但在Firefox中使其提交两次而不是一次。

Sometimes you don't have control of HTML being generated by some widget or a module, so you style things with CSS. 有时,您无法控制由某些小部件或模块生成的HTML,因此您可以使用CSS设置样式。 This was the case with me too. 我也是这样。 If found that in Chrome it works if you use 如果发现在Chrome中可以正常使用

visibility:hidden;

but not if you use 但是如果您使用

display:none;

In FireFox it works either way. 在FireFox中,无论哪种方式都可以工作。 Hope this will be helpful to somebody. 希望这对某人有帮助。

如果您使用按键处理程序,则不需要隐藏的提交按钮,因此只需将其删除即可。

I'm doing this way and without Submit button. 我这样做是没有提交按钮。

$(document).ready(function()
{
    $('#Username').keypress(function(e){
        if(e.which == 13){
            $('#CheckLogin').submit();
            }
        }); 
    $('#Password').keypress(function(e){
        if(e.which == 13){
            $('#CheckLogin').submit();
            }
        });
});

If you don't want to display it, then don't add it in html. 如果您不想显示它,则不要在html中添加它。

html: 的HTML:

<form id="form" onsubmit="my_submit()">
<input type="text" id="msg" />
</form>

javascript: javascript:

function my_submit(){
    var msg = $('#msg').val();
    ....
    submit_on_my_own(msg);
    ...
    return false;
}

works for me in chrome 在Chrome中为我工作

http://jsfiddle.net/qEAeN/ http://jsfiddle.net/qEAeN/

might need to double check your js 可能需要仔细检查您的js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM