[英]Returning true doesn't allow default action in Knockout “event” binding
默認情況下,淘汰賽將阻止事件采取任何默認操作。
....
但是,如果您確實想讓默認操作繼續進行,只需從您的事件處理函數中返回 true。
這似乎對我不起作用。 這是我的觀點:
<input data-bind="event: { 'keydown': function(d, e) { onInputKeydown(e); } }" />
和我的 ViewModel 中的事件處理程序:
onInputKeydown = function(e) {
console.log(e.which);
return true;
}
此處理程序阻止將任何字符輸入到<input>
元素中。 我究竟做錯了什么?
您的 ViewModel 中的函數確實返回 true,但您在視圖中指定的包裝器並未傳播該函數的返回值。 試試這個(注意添加return
):
<input data-bind="event: { 'keydown': function(d, e) { return onInputKeydown(e); } }" />
我認為問題在於你如何綁定函數。 請嘗試以下操作:
<input data-bind="event: { keydown: onInputKeydown }" />
Knockout 會將參數數據和事件傳遞給您的函數。
onInputKeydown = function(data, event) {
console.log(event.which);
return true;
}
是的,您確實需要從函數中返回 true。 我還發現,如果我在函數中遇到斷點,keydown 事件似乎會忽略我已返回 true。 禁用所有斷點,它工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.