简体   繁体   English

(Vue.js) 如何使用数据中的数值数组中的选项标签填充选择标签?

[英](Vue.js) How can I populate the select tag with option tag from array of number values in data?

I would like to populate my select tag with options from an array variable which contains arrays of number values.我想用包含数值数组的数组变量中的选项填充我的选择标记。 But the values that are reproduced seems to be blank但是复制出来的值好像是空白的

HTML: HTML:

  <select required id="dropDown">
    <option>Select here</option>
    <option v-for="choice in choices">{{ choice }}</option>
  </select>

Javascript: Javascript:

var vm = new Vue({   
el: 'body',    
data:{
    'choices': [1,2,3,4,5]
    }
});

Can someone point me of my mistake?有人可以指出我的错误吗? Because I am just a beginner though, and I would like to learn from you guys.因为我只是一个初学者,我想向你们学习。

The el option should provide Vue with an existing DOM element to mount on. el选项应该为 Vue 提供一个现有的 DOM 元素来挂载。 You have provided a CSS selector for body, so Vue will try to mount on the body element.您已经为 body 提供了一个 CSS 选择器,因此 Vue 将尝试挂载在body元素上。

Otherwise your code is correct.否则你的代码是正确的。 Just wrap your HTML in body tags and it works!只需将您的 HTML 包装在body标签中即可!

 var vm = new Vue({ el: 'body', data:{ 'choices': [1,2,3,4,5] } });
 <!-- Load Vue JS --> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.js"></script> <!-- add body tags so `el: 'body'` resolves to an HTML element --> <body> <select required id="dropDown"> <option>Select here</option> <option v-for="choice in choices">{{ choice }}</option> </select> </body>

If you get your data from the backend, you need to follow another way.如果您从后端获取数据,则需要遵循另一种方式。 For example, I have a category list, which comes from the backend.例如,我有一个来自后端的类别列表。

 <el-select
              v-model="categorySelect.selected"
              size="large"
              name="category_id"
              single
              v-bind:placeholder="$t('products.category')"
          >
            <el-option
                v-for="item in categorySelect.autocompleteItems"
                :key="item.id"
                :label="item.name_en"
                :value="item.id"
            ></el-option>
          </el-select>

Your script should looks like this :您的脚本应如下所示:

categorySelect: {
    selected: [],
    autocompleteItems: this.categoriesItem,
  },

categoriesItem: {
      required: false,
      type: Object
    },

and then, you need to get your data on your view like this :然后,您需要像这样在您的视图中获取数据:

:categories-item="{{$categories}}"

as you see the above code, you can fill your select list with a different list that comes from the backend.当您看到上面的代码时,您可以使用来自后端的不同列表填充您的选择列表。

Enjoy Your Code!享受您的代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能output<template></template> 标记到 Vue.js 中的 DOM - How can I output the <template /> tag to the DOM in Vue.js 如何在选项/选择标签循环之外的按钮中使用 Vue.js 中下拉列表的选定 id - How to use the selected id of a dropdown in Vue.js in a button outside of option/select tag loop 如何使用 vue.js 在 select 选项中添加数据图标属性 - How I can add data-icon attribute in select option using vue.js 如何制作 <select> html中的标记仅显示某些数据,具体取决于另一个<select>使用Vue.js标记 - How to make a <select> tag in html only shows some data depending on another <select> tag using Vue.js Vue.js - 如何在选择时删除数组元素<select>标签 - Vue.js - How to remove an array element upon selection by <select> tag 我可以使用 <tr> 里面的标签 <tr> 在Vue.js中? - Can I use a <tr> tag inside a <tr> in Vue.js? 如何在Vue JS中获得select标签的值 - How can I get the value of select tag in Vue JS 如何渲染数组以选择选项vue.js - How to render array to select option vue.js 使用来自xml链接的动态数据填充选择选项标签 - Populate select option tag with dynamic data from xml link 用户选择一个选项后,如何从选择标签中获取数据属性 - How can I get data-att from select tag after user select an option
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM