keydown事件不适用于jquery mobile中的输入类型搜索

问题描述 投票:1回答:3

我想在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
3个回答
1
投票

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

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


2
投票

包含jQuery Mobile文件

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

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

ResolveClientUrl

同时给路径。


0
投票

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

FIDDLE'd

:)
© www.soinside.com 2019 - 2024. All rights reserved.