如何在Vue 3中将列表中的选定值设置为输入值?

huangapple go评论50阅读模式
英文:

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:

&lt;input type=&quot;text&quot; name=&quot;project&quot; v-model=&quot;project&quot; /&gt;
      &lt;div :class=&quot;[&#39;result&#39;,{hidden:!results?.length}]&quot; &gt;
        &lt;ul&gt;
          &lt;li v-for=&quot;(result) in results&quot; @click=&quot;selectProject(result)&quot;&gt;
             {{ result.name}}
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;



let project = ref(&#39;&#39;);
let template = ref(&#39;&#39;);
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) =&gt; {
  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) =&gt; {
    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(&#39;&#39;);
    const template = ref(&#39;&#39;);
    const results = ref([
      { id: 1, name: &#39;Projekt 1&#39; },
      { id: 2, name: &#39;Projekt 2&#39; },
      { id: 3, name: &#39;Projekt 3&#39; }
    ]);
    const selectedProject = ref();    
    const selectProject = (value) =&gt; {
      project.value = value.name;
      selectedProject.value = value.id;
      results.value = [];
    } 
    return { 
      project,
      template,
      results, 
      selectedProject,
      selectProject
     }
  }  
}

const app = createApp(App);
app.mount(&#39;#app&#39;);

<!-- language: lang-css -->

#app { line-height: 1.5; }
[v-cloak] { display: none; }
li { cursor: pointer; text-decoration: underline; color: blue;}

<!-- language: lang-html -->

&lt;div id=&quot;app&quot; v-cloak&gt;  
  Select project: {{selectedProject}} &lt;br/&gt;
  &lt;input type=&quot;text&quot; name=&quot;project&quot; v-model=&quot;project&quot; /&gt;
  &lt;div :class=&quot;[&#39;result&#39;,{hidden:!results?.length}]&quot; &gt;
    &lt;ul&gt;
      &lt;li v-for=&quot;result in results&quot; @click=&quot;selectProject(result)&quot;&gt;
         {{result.name}}
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 03:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定