admin管理员组文章数量:1391989
Question originally posted (in Spanish) on es.stackoverflow by José:
I have seen the example in JavaScript, and yes, it works and everything but as I can do it in vue.js, I've been trying for a while and it does not work. Sorry for the inconvenience.
<script src=""></script> <div id="app"> <p>{{ name }}</p> <input type="text" v-model="name"/> <button type="submit" :disabled="name == defaultName">Submit</button> </div> >
Question originally posted (in Spanish) on es.stackoverflow. by José:
I have seen the example in JavaScript, and yes, it works and everything but as I can do it in vue.js, I've been trying for a while and it does not work. Sorry for the inconvenience.
<script src="https://unpkg./vue"></script> <div id="app"> <p>{{ name }}</p> <input type="text" v-model="name"/> <button type="submit" :disabled="name == defaultName">Submit</button> </div> >
Javascript
$(document).ready(function() { $('#id_categoria').change(function(e) { if ($(this).val() === "1") { $('#d').prop("disabled", true); } else { $('#d').prop("disabled", false); } }) });
HTML
<script src="https://ajax.googleapis./ajax/libs/jquery/1.8.3/jquery.min.js"></script> <div> <select name='id_categoria' id='id_categoria' > <option value="1" selected>Clientes</option> <option value="2">Empresas</option> <option value="3">Personas</option> </select> <input id="d" disabled="disabled" type="text" value="test"> </div>
I know it's weird that I want to directly manipulate the DOM when you use Vue; I usually let the framework do that, This would be a possible solution
Of course, if it's a possibilty, you should take advantage of Vue's data-driven reactive nature, as tackling with the DOM is always tricky.
The solution would be to just create another variable and populate it on
Share Improve this question edited Nov 24, 2020 at 10:36 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Jul 1, 2019 at 22:52 anonanon 3new Vue({ el: '#app', data: { name: 'Hello Vue.js!', defaultName: null }, mounted() { this.defaultName = this.name; }
Add a ment |
- This is because you're trying to use jQuery methods instead of using Vue. The two don't mix very well (it's very rare that you want to be directly manipulating the DOM when using Vue; generally you let the framework do that.) If you need help getting this sort of thing working within Vue you should include your Vue code in the question, not just the jQuery. – Daniel Beck Commented Jul 1, 2019 at 22:58
- With Vue you won't use a
document.ready
function, you'll have a javascript object which is your model, and your select and input controls will be bound to elements in that model. See Form Input Bindings for an overview. Your<input>
will probably look something like<input type="text" id="d" :disabled="categoria==1">
– Stephen P Commented Jul 1, 2019 at 22:58- Copied without attribution from habilitar/deshabilitar un input con un select – Martijn Pieters Commented Nov 20, 2020 at 10:26
3 Answers
Reset to default 3Below is a vue template file using v-model instead of bining jQuery
<template> <div> <select v-model="id_categoria"> <option value="1" :selected="id_categoria === 1">Clientes</option> <option value="2" :selected="id_categoria === 2">Empresas</option> <option value="3" :selected="id_categoria === 3">Personas</option> </select> <input id="d" :disabled="id_categoria === 1" type="text" value="test"> </div> </template> <script> export default { data() { return { id_categoria: 1, }; }, } </script>
Try this:
new Vue({ el: "#app", data: { options: [ { content: 'Clientes', value: 1 }, { content: 'Empresas', value: 2 }, { content: 'Personas', value: 3 } ], inputDisabled: true }, methods: { onChangeSelect(e) { this.inputDisabled = (e.target.value == 1) } } })
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <select @change="onChangeSelect($event)"> <option v-for="(option, index) in options" :key="index" :value="option.value"> {{ option.content }} </option> </select> <input :disabled="inputDisabled" type="text" value="test"> </div>
new Vue({ el: "#app", data: { options: [ { content: 'Clientes', value: 1 }, { content: 'Empresas', value: 2 }, { content: 'Personas', value: 3} ], selectedOption: '', }, puted: { inputDisabled() { return this.selectedOption === 2; } } })
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vue.js"></script> <div id="app"> <select v-model="selectedOption"> <option v-for="(option, index) in options" :key="index" :value="option.value">{{ option.content }}</option> </select> <input :disabled="inputDisabled" type="text" v-model="selectedOption"> </div>
本文标签: javascriptHow to enabledisable an input with a select VueJSStack Overflow
版权声明:本文标题:javascript - How to enabledisable an input with a select VueJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744697960a2620393.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论