我正在努力解決第一次使用knockoutjs時出現的問題。敲除捕捉在文本框中輸入以實現與按鈕相同
我有一個搜索字符串文本框:
<input type="text" data-bind="value: searchString" id="searchText">
和一個按鈕:
<span data-bind="click: searchButton" id="searchBtn"></span>
都採用淘汰賽使用以下腳本綁定:
function ViewModel() {
var self = this;
//-----
self.searchButton = function() {
if (self.searchString() != null && self.searchString().length > 3) {
$.ajax({
type: "post",
contentType: "application/json",
url: "./SearchCustomer/",
data: "{'searchString':'" + self.searchString() + "'}",
error: function (xhr, status, error) {
baseShowError("Error");
},
success: function (response) {
var receivedResponse = JSON.parse(response);
if (receivedResponse.Success) {
ko.mapping.fromJS(receivedResponse.Result, {}, self);
} else {
baseShowError("customer not found");
}
}
});
}
};
});
$(function() {
var jsonModel = '@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(this.Model, new Newtonsoft.Json.Converters.IsoDateTimeConverter()))';
var myViewModel = ko.mapping.fromJSON(jsonModel, {}, new ViewModel());
ko.applyBindings(myViewModel);
});
現在我想用腳本在文本框中輸入一個輸入:
$(document).ready(function() {
$('#searchText').keypress(function (e) {
if (e.which == 13) {
...........
}
});
});
唯一的問題是輸入什麼而不是點。我讀過關於不引人注意的事件處理,但沒有試過它似乎工作。有人可以給我一個提示嗎?
這個答案讓我很失望。您通過引入不必要的額外HTML元素來「解決」了問題。現在你有兩條事件路徑可以處理(他們是否在你的表單中輸入了某處?或者他們點擊了'searchButton') –