回答
想這問題是已經回答了。 您可能需要創建只接受允許的字符的自定義綁定。
這個問題的答案在此鏈接後會幫助你:make an input only-numeric type on knockout
@Seminda給出的答案比你鏈接的問題的答案要好得多。自定義綁定不是理想的解決方案,擴展器更加靈活,並且提供更好的代碼分離(您的HTML不應該規定您的領域邏輯,JavaScript應該)。 – 2014-08-28 08:51:55
你可以寫在你的視圖模型的功能。
ko.extenders.numeric = function(target, precision) {
//create a writable computed observable to intercept writes to our observable
var result = ko.pureComputed({
read: target, //always return the original observables value
write: function(newValue) {
var current = target(),
roundingMultiplier = Math.pow(10, precision),
newValueAsNum = isNaN(newValue) ? 0 : parseFloat(+newValue),
valueToWrite = Math.round(newValueAsNum * roundingMultiplier)/roundingMultiplier;
//only write if it changed
if (valueToWrite !== current) {
target(valueToWrite);
} else {
//if the rounded value is the same, but a different value was written, force a notification for the current field
if (newValue !== current) {
target.notifySubscribers(valueToWrite);
}
}
}
}).extend({ notify: 'always' });
//initialize with current value to make sure it is rounded appropriately
result(target());
//return the new computed observable
return result;
};
檢查此鏈接如何使用:Sample code
這是迄今爲止最好的方法。 (也比鏈接@ Raghavendra-N發佈的接受的答案更好)。這主要是因爲兩個原因:擴展器比自定義綁定更靈活,並且它促進了更好的代碼分離。因此,我的意思是將數字限制在JavaScript中是比較合理的,因爲它代表域邏輯,而不是在與呈現相關的HTML中。 – 2014-08-28 08:49:58
嗨漢斯,你能回答這個問題嗎?http://stackoverflow.com/questions/25637663/converting-circular-structure-to-json-in-knockout-js – user2142786 2014-09-03 07:06:21
- 1. 如何啓用/使用禁用輸入字段淘汰賽JS
- 2. 與淘汰賽JS
- 3. 淘汰賽js applybinding
- 4. 防止輸入字符的輸入型數與淘汰賽
- 5. 淘汰賽由淘汰賽
- 6. 驗證在淘汰賽JS
- 7. 在淘汰賽的js
- 8. 下鑽在淘汰賽JS
- 9. 關係在淘汰賽,JS
- 10. BreezeJS +淘汰賽不能設置輸入字段設置爲null
- 11. 淘汰賽JS加/內ko.utils.arrayForEach
- 12. 淘汰賽JS錯誤:
- 13. ASP.NET MVC4和淘汰賽js
- 14. 傳值淘汰賽JS
- 15. 淘汰賽JS的foreach
- 16. Claryfying約淘汰賽JS
- 17. 淘汰賽JS建議
- 18. 淘汰賽JS變量超越控制
- 19. 淘汰賽JS從MVC控制器
- 20. 在淘汰賽
- 21. 在淘汰賽
- 22. 如何通過價值淘汰賽js
- 23. 如何?與淘汰賽JS驗證
- 24. 淘汰賽的foreach數字陣列
- 25. 淘汰賽自定義數字綁定
- 26. 淘汰賽JS,到一個數組
- 27. 淘汰賽js倒數計時器
- 28. 獲取URL參數與淘汰賽JS
- 29. 淘汰賽JS收集數據
- 30. 淘汰賽和表格的輸入值
利用基因敲除驗證 – 2014-08-28 07:01:45
通過@Seminda這裏給出的答案比你鏈接的問題的答案要好得多。 – 2014-08-28 08:50:56