我正在尝试使用enterkey在textarea中提交文本。工作正常,但在提交数据之前,enterkey总是添加一个新行......
我知道我必须使用event.preventDefault()但它对我不起作用。
ko.bindingHandlers.enterKey = {
init: function (element, valueAccessor, allBindingsAccessor, data) {
var wrappedHandler, newValueAccessor;
// wrap the handler with a check for the enter key
wrappedHandler = function (data, event) {
if (event.keyCode === ENTER_KEY && !event.shiftKey) {
event.target.blur();
valueAccessor().call(this, data, event);
}
};
// create a valueAccessor with the options that we would want to pass to the event binding
newValueAccessor = function () {
return {
keyup: wrappedHandler
};
};
// call the real event binding's init function
ko.bindingHandlers.event.init(element, newValueAccessor, allBindingsAccessor, data);
}
};
答案 0 :(得分:0)
您应该在event.preventDefault
事件上致电keypress
例如,下面的代码会阻止按键输入
ko.bindingHandlers.enterKey = {
init: function (element, valueAccessor, allBindingsAccessor, data) {
$(element).keypress(function(e) {
(e.keyCode == 13) {
e.preventDefault();
// do something else
}
})
}
}