gpt4 book ai didi

javascript - Vue 在将其设置为禁用时重置 INPUT 字段

转载 作者:搜寻专家 更新时间:2023-10-30 22:35:54 24 4
gpt4 key购买 nike

为什么 Vue 在将文本输入字段设置为禁用时会重置它?这不会发生在纯 Javascript 中,也不会发生在文本区域中。

var app = new Vue({
el: '.container',
data: {
disabled: false,
fn: "John",
ln: "Smith",
com: "My comment here..."
},
methods: {
vue_disable() {
this.disabled = !this.disabled;
}
}
});

function js_disable() {
document.getElementById("first_name").disabled = !document.getElementById("first_name").disabled;
document.getElementById("comment").disabled = !document.getElementById("comment").disabled;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container">
<h1>Vue disable fields</h1>

<p>22 Aug 2019</p>

<p>Why does an INPUT text field reset when I set the field to disabled with Vue? It doesn't happen with pure Javascript, and doesn't happen with a TEXTAREA.</p>

<p>Type something new into the the First Name field then click the first button. The value you entered disappears and the original value is substituted.<p>

<form>
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" id="first_name" :disabled="disabled" :value="fn"/>
</div>
<div class="form-group">
<label>Last name</label>
<input type="text" class="form-control" id="last_name" :value="ln"/>
</div>
<div class="form-group">
<label>Comment</label>
<textarea rows="4" class="form-control" id="comment" v-html="com" :disabled="disabled">

</textarea>
</div>
<p>
<button type="button" class="btn btn-default" @click="vue_disable">Disable / Enable 'First Name' and 'Comment' fields with Vue</button>
</p>
</form>
<p>
<button type="button" class="btn btn-default" onclick="js_disable()">Disable / Enable 'First Name' and 'Comment' fields with Javascript</button>
</p>

</div>

https://codepen.io/MSCAU/pen/jONVRRj

最佳答案

使用v-model代替:value

来自documentation :

Use the v-model directive to create two-way data bindings on form input, textarea, and select elements.

value 属性只是设置输入的初始值。

Vue 使用 Object.defineProperty 将 getter/setter 添加到 data 中的所有属性。当依赖项的 setter 被触发时,它会通知观察者,并重新呈现组件 ( documentation )。由于您更改了 disabled 属性,整个组件重新呈现并且 value 设置为 data

中的任何内容

var app = new Vue({
el: '.container',
data: {
disabled: false,
fn: "John",
ln: "Smith",
com: "My comment here..."
},
methods: {
vue_disable() {
this.disabled = !this.disabled;
}
}
});

function js_disable() {
document.getElementById("first_name").disabled = !document.getElementById("first_name").disabled;
document.getElementById("comment").disabled = !document.getElementById("comment").disabled;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container">
<p>
<form>
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" id="first_name" :disabled="disabled" v-model="fn" />
</div>
<div class="form-group">
<label>Last name</label>
<input type="text" class="form-control" id="last_name" v-model="ln" />
</div>
<div class="form-group">
<label>Comment</label>
<textarea rows="4" class="form-control" id="comment" v-html="com" :disabled="disabled">
</textarea>
</div>
<p>
<button type="button" class="btn btn-default" @click="vue_disable">Disable / Enable 'First Name' and 'Comment' fields with Vue</button>
</p>
</form>
<p>
<button type="button" class="btn btn-default" onclick="js_disable()">Disable / Enable 'First Name' and 'Comment' fields with Javascript</button>
</p>

</div>

关于javascript - Vue 在将其设置为禁用时重置 INPUT 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57604402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com