英文:
How can I set the selected value from a list to an input in vue 3?
问题
以下是代码部分的中文翻译:
<input type="text" name="project" v-model="project" />
<div :class="['result', {hidden:!results?.length}]">
<ul>
<li v-for="(result) in results" @click="selectProject(result)">
{{ result.name }}
</li>
</ul>
</div>
let project = ref('');
let template = ref('');
let results = ref([]);
let selectedProject = ref(null);
// 这是我尝试在单击结果列表中的选项时设置所选值的地方
const selectProject = (project) => {
project.value = project.name;
selectedProject.value = project.id;
results.value = [];
}
英文:
I have list that is returned from a request, I have a text input where I search and the list show the results of that search, I want to choose an option from that list I show the selected value in the put where i did the search.
I Tried this but it doesn't show the selected value:
<input type="text" name="project" v-model="project" />
<div :class="['result',{hidden:!results?.length}]" >
<ul>
<li v-for="(result) in results" @click="selectProject(result)">
{{ result.name}}
</li>
</ul>
</div>
let project = ref('');
let template = ref('');
let results = ref([])
let selectedProject = ref(null);
this is where I tried to set the selected value when i click one of the options from the result list
const selectProject = (project) => {
project.value = project.name
selectedProject.value = project.id
results.value = []
}
答案1
得分: 1
Mr. R是正确的。这是修复。
const selectProject = (value) => {
project.value = value.name;
selectedProject.value = value.id;
results.value = [];
}
Playground
<div id="app" v-cloak>
选择项目: {{selectedProject}} <br/>
<input type="text" name="project" v-model="project" />
<div :class="['result',{hidden:!results?.length}]" >
<ul>
<li v-for="result in results" @click="selectProject(result)">
{{result.name}}
</li>
</ul>
</div>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
英文:
Mr. R is right. Here is the fix.
const selectProject = (value) => {
project.value = value.name;
selectedProject.value = value.id;
results.value = [];
}
Playground
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const { createApp, ref } = Vue;
const App = {
setup() {
const project = ref('');
const template = ref('');
const results = ref([
{ id: 1, name: 'Projekt 1' },
{ id: 2, name: 'Projekt 2' },
{ id: 3, name: 'Projekt 3' }
]);
const selectedProject = ref();
const selectProject = (value) => {
project.value = value.name;
selectedProject.value = value.id;
results.value = [];
}
return {
project,
template,
results,
selectedProject,
selectProject
}
}
}
const app = createApp(App);
app.mount('#app');
<!-- language: lang-css -->
#app { line-height: 1.5; }
[v-cloak] { display: none; }
li { cursor: pointer; text-decoration: underline; color: blue;}
<!-- language: lang-html -->
<div id="app" v-cloak>
Select project: {{selectedProject}} <br/>
<input type="text" name="project" v-model="project" />
<div :class="['result',{hidden:!results?.length}]" >
<ul>
<li v-for="result in results" @click="selectProject(result)">
{{result.name}}
</li>
</ul>
</div>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论