[英]Jquery Mobile Listview Autocomplete: how to trigger function when user presses enter?
I'm following the jquery mobile remote autocomplete demo: 我正在关注jquery移动远程自动完成演示:
http://jquerymobile.com/demos/1.3.0-beta.1/docs/demos/listviews/listview-filter-autocomplete.html http://jquerymobile.com/demos/1.3.0-beta.1/docs/demos/listviews/listview-filter-autocomplete.html
My list is being dynamically populated from my datasource fine and I can do things when the user clicks on a result from the list. 我的列表正在从我的数据源中动态填充,当用户单击列表中的结果时,我可以执行操作。
However I also need it to trigger a function when the user hits enter (or clicks “Go” on the phone)... How can I do this? 但是我还需要它在用户点击进入时触发功能(或点击手机上的“Go”)...我该怎么做? Here's my current code:
这是我目前的代码:
$( document ).on( "pageinit", "#myPage", function() {
$( "#autocomplete" ).on( "click","li",function() {
// do stuff when user clicks on item in list
alert('Doing stuff!');
});
$( "#autocomplete" ).on( "listviewbeforefilter", function ( e, data ) {
var $ul = $( this ),
$input = $( data.input ),
value = $input.val(),
html = "";
$ul.html( "" );
if ( value && value.length > 2 ) {
$ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
$ul.listview( "refresh" );
$.ajax({
url: "http://mywebservice/"+$input.val(),
dataType: "json",
crossDomain: false
})
.then( function ( response ) {
$.each( response, function ( i, val ) {
html += "<li><a href='#'>" + val.display_name + "</a></li>";
});
$ul.html( html );
$ul.listview( "refresh" );
$ul.trigger( "updatelayout");
});
}
});
});
I've been searching alot for help, but most results have been talking about the jquery autocomplete and not the jquery mobile listview autocomplete... 我一直在寻找很多帮助,但大多数结果都是关于jquery自动完成而不是jquery移动列表视图自动完成...
Any help would be much appreciated -thanks! 任何帮助将不胜感激 - 谢谢!
Hey I used a local autocomplete jQM widget but this will work the same for ya - 嘿我使用了一个本地自动完成jQM小部件,但这对你来说也是一样的 -
HTML - HTML -
<div data-role="page" id="carPage">
<div data-role="content">
<ul id="autocomplete" data-role="listview" data-inset="true" data-filter="true" data-filter-placeholder="Find a car..." data-filter-theme="d">
<li><a href="acura.html">Acura</a></li>
<li><a href="audi.html">Audi</a></li>
<li><a href="bmw.html">BMW</a></li>
<li><a href="bmw.html">Cadillac</a></li>
<li><a href="bmw.html">Ferrari</a></li>
<li><a href="bmw.html">Honda</a></li>
</ul>
</div>
</div>
JS - JS -
$(function () {
$('#carPage input[data-type="search"]').on('keydown', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) { //Enter keycode
alert('enter key was pressed');
}
});
});
/Update /更新
Regarding the go button - Because the autocomplete
widget wraps a form element around your content the go button will trigger a submit
on the form
. 关于go按钮 - 因为
autocomplete
小部件在表单元素周围包含表单元素,所以go按钮将触发form
上的submit
。 This means you can listen to the enter key press and go button press with this simple event handler like this below - 这意味着您可以使用这个简单的事件处理程序按下回车键和按下按钮,如下所示 -
$("#carPage form").submit(function() {
// this will handle both the enter key and go button on device
});
I updated the jsFiddle demo with both approaches above. 我用上述两种方法更新了jsFiddle演示。 I like the second approach best because it handles both scenarios the easiest.
我最喜欢第二种方法,因为它最容易处理两种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.