【问题标题】:keydown event is not working for input type search in jquery mobilekeydown 事件不适用于 jquery mobile 中的输入类型搜索
【发布时间】:2013-01-31 12:06:11
【问题描述】:

我想为 jquery mobile 中的输入类型搜索调用 keydown 事件。下面是我的html代码。

 <!DOCTYPE html>
<html>
 <head>
    <meta charset="ISO-8859-1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Insert title here</title>
<link rel="stylesheet" href="jquery.mobile-1.2.0.min.css">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {      
    $("#search1").keydown(function() {
        alert("keydown");           
        $("#search1").css("background-color", "yellow");
    });
    $("#search1").keyup(function() {
        alert("keyup");
        $("#search1").css("background-color", "pink");
    });
});
</script>
<script type="text/javascript" src="jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<div data-role="page">

<div data-role="content">
<input type="search" id="search1">
<input type="text" id="text1">
</div>
</div>
</body>
</html>

keydown 事件正在为所有其他输入类型触发,搜索类型除外。有人可以帮忙吗?

【问题讨论】:

标签: javascript jquery html jquery-mobile keydown


【解决方案1】:

包含 jQuery Mobile 文件

您需要做的只是在您的项目中包含一个 jQuery 文件。一个名为 jQuery.mobile.js 或任何版本的非常相似的文件(例如 jQuery.ui.js)可以帮助你。

您可以从以下地址下载:jQuery Mobile Official | Download 我建议使用

ResolveClientUrl

同时给出路径。

【讨论】:

    【解决方案2】:

    docs 发现这个:http://jquerymobile.com/test/docs/api/events.html

    Important: Use $(document).on('pageinit'), not $(document).ready()

    $(document).on( 'pageinit',function(event){
        $("#search1").keydown(function() {
            alert("keydown");           
            $("#search1").css("background-color", "yellow");
        });
        $("#search1").keyup(function() {
            alert("keyup");
            $("#search1").css("background-color", "pink");
        });
    });
    

    试试这个,看看是否有帮助。

    【讨论】:

    • 嗨 Jai,它在 pageinit 函数中运行良好,但在 document.ready 中却不行。和
    • 有没有办法在jquery脚本中获取输入的搜索查询字符串?
    • 尝试为此使用 jquery:var iVal = $("#search1").val(); alert(iVal);
    • 您好,我尝试过这种方式,但它给出的字符比我们输入的少一个。例如:如果我们输入“hai”,提醒它只给出“ha”
    • 我们需要写在keyup中,而不是keydown中。非常感谢您的帮助:)
    【解决方案3】:

    你的代码在这里工作检查:JS Fiddle

    FIDDLE'd

    :)
    

    【讨论】:

    • 谢谢 Sandeep,它在 jsFiddle 中运行良好 :) ,但在 Chrome 中却不行 :(
    猜你喜欢
    • 2013-09-08
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多