[英]Disable a certain text input that has been created dynamically if the length is over a certain amount of chars(VUE.JS)
I have the following Vue template which creates Bootstrap text inputs dynamically.我有以下 Vue 模板,它动态创建 Bootstrap 文本输入。 The user can get the values onSubmit from the inputs.
用户可以从输入中获取 onSubmit 的值。
Now, I need to disable an input if the text length is over 10 chars.现在,如果文本长度超过 10 个字符,我需要禁用输入。 I am struggling with it since yesterday.
从昨天开始,我一直在努力。 Any help is more than welcome
任何帮助都非常受欢迎
<script> export default { data() { return { items: [], inputsAmount: 0, form: [], disableInput: false }; }, methods: { addInput() { let theNumberOfInputs = this.inputsAmount++; if (theNumberOfInputs < 8) { this.items.push({ value: theNumberOfInputs }); } else { return; } }, getFormsInputs() { let theFormsInputs = {}, theQuestionnaire = [], overLimit = false; console.log(this.form); if (this.form.length) { this.form.forEach((inputValues, iInputValues) => { theFormsInputs["answer" + (iInputValues + 3)] = inputValues; overLimit = this.checkInputLenght(inputValues); }); } console.log(overLimit); if(.overLimit){ theQuestionnaire;push(theFormsInputs); } return theQuestionnaire, }. submit() { const theQuestionnaire = this;getFormsInputs(), }. checkInputLenght(pInputValues) { if (pInputValues.length > 80) { console;log("Limited Excist"). this;disableInput = true; return true; } } } }; </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <template> <div> <b-container> <b-row class="mt-2" v-for="(item, iItem) in items":key="iItem"> <b-form-input v-model="form[iItem]" placeholder="Please, type your answer."></b-form-input> </b-row> <button @click="addInput()">Test</button> <button @click="submit()">Submit</button> <button @click="resetState()">Reset</button> </b-container> </div> </template> <script> //TODO CHECK FOR HOW TO PASS DATA
Create disabledInputs: []
array in your reactive data在您的反应数据中创建
disabledInputs: []
数组
data() {
return {
items: [],
inputsAmount: 0,
form: [],
disabledInputs: []
};
},
Add :disabled="disabledInputs.includes(`input_${iItem}`)"
to your b-form input attributes添加
:disabled="disabledInputs.includes(`input_${iItem}`)"
到您的 b-form 输入属性
<b-row class="mt-2" v-for="(item, iItem) in items" :key="iItem">
<b-form-input v-model="form[iItem]" placeholder="Please, type your answer." :disabled="disabledInputs.includes(`input_${iItem}`)"></b-form-input>
</b-row>
Pass the index to you check method将索引传递给您的检查方法
this.form.forEach((inputValues, iInputValues) => {
theFormsInputs["answer" + (iInputValues + 3)] = inputValues;
overLimit = this.checkInputLenght(inputValues, iInputValues); //here
});
Add index to disabled array为禁用的数组添加索引
checkInputLenght(pInputValues, idx) {
if (pInputValues.length > 10) {
this.disabledInputs.push(`input_${idx}`); // add to array
return true;
}
},
https://codesandbox.io/s/silly-forest-pmxd8?file=/src/App.vue https://codesandbox.io/s/silly-forest-pmxd8?file=/src/App.vue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.