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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14625409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:09:05  来源:igfitidea点击:

keydown event is not working for input type search in jquery mobile

javascriptjqueryhtmljquery-mobilekeydown

提问by Sriks

I want to invoke keydown event for input type search in jquery mobile. Below is my html code.

我想在 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 event is getting fired for all the other input types, except for search type. Can someone please help on this?

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

采纳答案by Jai

From the docsfound this: http://jquerymobile.com/test/docs/api/events.html

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

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

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

Tryout this and see if it helps.

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

回答by Devang Kamdar

Include jQuery Mobile file

包含 jQuery Mobile 文件

What you need to do is just include a jQuery file into your project. A file named jQuery.mobile.js or quite similar (ex. jQuery.ui.js) of any version can help you.

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

You can download it from : jQuery Mobile Official | DownloadI suggest to use

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

ResolveClientUrl

解析ClientUrl

while giving path.

同时给出路径。

回答by Sandeep Rao

Your code is working check here: JS Fiddle

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

FIDDLE'd

小提琴的

:)