I've set up my Knockout bindings to have a keypress event because I wish to detect Enter key event on an input field. its working fine at sometimes only, not at all the times.
Code:
<input name="" id="txtSearch" placeholder="" value="" type="search" data-bind="event: { keypress: $root.SendMsg }" />
Viewmodel:
self.SendMsg = function (data, event) {
try {
if (event.which == 13) {
var SearchText = $("#txtSearch").val();
$(".divLoading").show();
$.ajax({
url: 'http://localhost/api/contacts/search',
type: 'GET',
dataType: 'jsonp',
data: { Text: SearchText },
context: this,
success: function (result) {
self.Contacts(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$(".divLoading").hide();
alert(errorThrown);
},
complete: function () {
$('#ListSearch').listview('refresh');
}
});
return false;
}
return true;
}
catch (e)
{
alert(e);
}
}
};
Data are coming from api successfully but text enter is working sometimes and not working sometimes. If we refresh the page then its working fine. I don't know why. Please help me friends