我正在开发一个具有不同许可类型的应用程序,根据许可证,我们需要禁用/启用输入.
一种方法是 for each 输入输入一个条件:disabled
,但这需要大量工作,而且容易出错,因为我们可能会忘记在某些输入上输入它.
我曾想过使用一个像v-disable-all
这样的指令来搜索容器下的所有输入,并向其中添加disabled.
我在想,是有更好的解决方案,还是已经有这样的解决方案?
我正在开发一个具有不同许可类型的应用程序,根据许可证,我们需要禁用/启用输入.
一种方法是 for each 输入输入一个条件:disabled
,但这需要大量工作,而且容易出错,因为我们可能会忘记在某些输入上输入它.
我曾想过使用一个像v-disable-all
这样的指令来搜索容器下的所有输入,并向其中添加disabled.
我在想,是有更好的解决方案,还是已经有这样的解决方案?
我最终制定了这个指令:
import Vue from "vue";
Vue.directive("disable-all", {
// When all the children of the parent component have been updated
componentUpdated: function(el, binding) {
if (!binding.value) return;
const tags = ["input", "button", "textarea", "select"];
tags.forEach(tagName => {
const nodes = el.getElementsByTagName(tagName);
for (let i = 0; i < nodes.length; i++) {
nodes[i].disabled = true;
nodes[i].tabIndex = -1;
}
});
}
});