使用回车键/隐藏提交按钮提交表单

时间:2012-04-13 19:35:31

标签: jquery

我正在创建一个带有单个输入字段的jquerymobile页面来进行数据库搜索。输入由条形码阅读器完成,该阅读器最后按下输入。我想隐藏提交按钮,并使用回车键提交表单。

我尝试过在其他线程中读取的一些代码,但无济于事。在谈到javascript时,我很高兴。

<script type="text/javascript" language="JavaScript">
    $(document).ready(function() {
        $('#submit').hide();
    });
$('.noEnterSubmit').keypress(function(e){
    if ( e.which == 13 ) 
    $('form#search').submit();
    e.preventDefault();
    return false;
});
</script>

最重要的是,我有一个按钮,按下时会关注输入字段(因为在iOS设备上你不能强调聚焦onload)

<form action="#CGI.SCRIPNAME#" method="post" name="search">
<label for="season_pass_id">Season Pass ID</label>
<input class="noEnterSubmit" type="password" name="season_pass_id">
<input id="submit" type="submit" name="submit" value="submit" >
<input type="button" name="mybutton" value="Scan New" OnClick="document.search.season_pass_id.focus();">
</form>

最终我想隐藏输入字段和提交按钮,只有“扫描新建”​​按钮

1 个答案:

答案 0 :(得分:0)

尝试将您的按键代码移动到document.ready处理程序中。我怀疑当你附加事件时dom元素没有被加载。

$(document).ready(function() {
    $('#submit').hide();
    $('.noEnterSubmit').keypress(function(e){
       e.preventDefault(); 
       if ( e.which == 13 ) 
        $('form#search').submit();    
    });
});