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

旧时模样 提交于 2019-12-08 07:53:18

问题


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

 <!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?


回答1:


From the docs found this: 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");
    });
});

Tryout this and see if it helps.




回答2:


Include jQuery Mobile file

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.

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

ResolveClientUrl

while giving path.




回答3:


Your code is working check here: JS Fiddle

FIDDLE'd

:)


来源:https://stackoverflow.com/questions/14625409/keydown-event-is-not-working-for-input-type-search-in-jquery-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!