【发布时间】:2021-11-10 08:42:04
【问题描述】:
我正在制作一个应用程序,您可以在其中将字符串添加到列表并进行编辑。当我点击编辑时,<h3> 由于一些 v-if/v-show 变成了一个输入字段,我想添加一个功能,当输入出现时,输入也立即获得焦点。
这是我当前的代码:
<div class="card" v-for="(item, index) in list" :key="index">
<!-- not editing -->
<div v-if="editing != index + 1">
<button class="edit-btn" @click="setEditing(item, index)">
edit
</button>
<h3 class="title">{{ index + 1 + ". " + item }}</h3>
<button class="delete-btn" @click="deleteEntry(index)">
bin
</button>
</div>
<!-- editing -->
<div v-show="editing == index + 1">
<button
class="edit-btn"
style="background-color:white;color:grey;border-color:grey;font-weight:bold"
@click="cancelEdit"
>
x
</button>
<input
ref="editInput"
autocomplete="off"
@change="console.log(entry)"
class="edit-input"
id="edit-input"
@keyup.enter="saveChanges(index)"
v-model="entry"
/>
<button
class="delete-btn"
style="background-color:white;border-color:green;color:green"
@click="saveChanges(index)"
>
mark
</button>
</div>
</div>
功能
setEditing(entry, index) {
this.editing = index + 1;
this.entry = entry;
var el = this.$refs.editInput[index];
console.log(el);
el.focus();
// document.getElementById("edit-input").focus();
},
变量
data() {
return {
editing: 0,
newEntry: "",
list: [],
error: "",
entry: "",
};
},
【问题讨论】:
标签: javascript vue.js input focus