简体   繁体   English

我怎样才能使淘汰赛“价值”绑定只写,这样它就不会尝试从中读取?

[英]How can I make a knockout "value" binding write-only so it doesn't try to read from it?

<input data-bind="value: fileName" type="file" multiple="">
<input data-bind="value: fileName" type="text" readonly="">

This works except I get:这有效,但我得到:

Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.

because it tries to set the value of the file input when I only want to read it and write it to the text input.因为当我只想读取它并将其写入文本输入时,它会尝试设置文件输入的值。

Example例子

http://jsfiddle.net/esk5akwj/2/ http://jsfiddle.net/esk5akwj/2/

  • Check box.复选框。
  • Choose a file.选择一个文件。
  • Check box again.再次选中复选框。
  • Try to choose a file again.再次尝试选择一个文件。

Since the file chooser value can only be set by using the file chooser, and must start as the empty string, you need a custom binding handler that wraps the value binding and on init sets the bound value to the empty string:由于文件选择器值只能使用文件选择器设置,并且必须以空字符串开头,因此您需要一个自定义绑定处理程序来包装value绑定,并在 init 上将绑定值设置为空字符串:

 ko.bindingHandlers.resetValue = { init: function(element, valueAccessor, allBindingsAccessor, data, context) { valueAccessor()(''); ko.bindingHandlers.value.init(element, valueAccessor, allBindingsAccessor, data, context); }, update: function(element, valueAccessor, allBindingsAccessor, data, context) { ko.bindingHandlers.value.update(element, valueAccessor, allBindingsAccessor, data, context); } }; ko.applyBindings({ fileName: ko.observable(), c: ko.observable() });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <input data-bind="checked: c" type="checkbox">Check me, choose a file, uncheck me, check me, choose a file. <!-- ko if: c --> <input data-bind="resetValue: fileName" type="file" multiple=""> <input data-bind="value: fileName" type="text" readonly> <!-- /ko -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当选择下拉列表中的值时,如何使用淘汰赛使div可见? - How can I, using Knockout, make a div only viewable when a value from a dropdown is selected? “值”绑定不适用于Knockout中的optgroup - 'Value' binding doesn't work with optgroup in Knockout 我如何发布一个参数,使其不出现在查询字符串中,以确保用户不能直接在URL中更改该值? - How can I POST a parameter so that it doesn't show up in the querystring, to make sure users can't change the value directly in the URL? 淘汰赛js:无法从数组中的表单写入值 - Knockout js: can't write value from form inside array 我如何解决这个jQuery故障,以便在使用.hover()时div不会消失? - How can I fix this jQuery glitch so the div doesn't try to disappear when using .hover()? 如何调试敲除中的模板绑定? - How can I debug, templated binding in knockout? 如何将值从 Javascript 文件返回到 Node.js,以便我可以写入文件? - How can I return a value from a Javascript file to a Node.js so I can write to a file? 我怎样才能使这个js脚本不使用eval? - How can I make this js script so that it doesn't use eval? 为什么协方差/逆变意味着只读/只写? - Why do covariance/contravariance imply read-only/write-only? 淘汰赛:无法解析绑定 - Knockout: Can't parse binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM