猿问

获取 p-tag 内的文本并存储在变量中

你们如何获取p 标签中的文本并将其存储在 VueJS 中的变量中?


因为现在,它只是将它显示在p 标签中,但我希望 p 标签内的文本存储在一个变量中以备后用。


下面是我的html代码


<b-form-input v-model="search" placeholder="Search by disease"></b-form-input>


<select v-model="selected">

  <option v-for="result in filteredPeople(search)" :key="result.LONG_D" :value="{ id: result.LONG_D, text: result.ICD_C }">{{ result.LONG_D }}</option>

</select>

<p>

  Value: {{selected.id}}

</p>

<b-button class="btn" variant="success" v-on:click="runAPI(search)">Search</b-button>

这是我的JS代码。


export default {

  data() {

    return {

      results: {},

      search: "",

      msg: null,

      selected: ""

    };

  },

  methods: {

    runAPI: function(disease) {

      axios

        .get("http://localhost:9000/api/disease/" + disease)

        .then(response => (this.results = response.data))

        .catch(error => console.log(error));

      console.log(this.results);

    },



    filteredPeople() {

      if (this.searchQuery) {

        return this.results.filter(item => {

          return item.LONG_D.startsWith(this.searchQuery);

        });

      } else {

        return this.results;

      }

    }

  }


千巷猫影
浏览 226回答 2
2回答

墨色风雨

你可以这样做。 代码&nbsp;export default {&nbsp; data() {&nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; results: {},&nbsp; &nbsp; &nbsp; search: "",&nbsp; &nbsp; &nbsp; msg: null,&nbsp; &nbsp; &nbsp; selected: ""&nbsp; &nbsp; };&nbsp; },&nbsp;methods: {runAPI: function(disease) {&nbsp; axios&nbsp; &nbsp; .get("http://localhost:9000/api/disease/" + disease)&nbsp; &nbsp; .then(response => (this.results = this.filteredPeople()))&nbsp; &nbsp; .catch(error => console.log(error));&nbsp; console.log(this.results);},filteredPeople() {&nbsp; if (this.searchQuery) {&nbsp; &nbsp; return this.results.filter(item => {&nbsp; &nbsp; &nbsp; return item.LONG_D.startsWith(this.searchQuery);&nbsp; &nbsp; });&nbsp; } else {&nbsp; &nbsp; return this.results;&nbsp; }代码<select v-model="selected">&nbsp; &nbsp;<option&nbsp; &nbsp; v-for="result in results"&nbsp; &nbsp; :key="result.LONG_D"&nbsp; &nbsp; :value="{{ id: result.LONG_D, text: result.ICD_C }}">{{ result.LONG_D }}</option></select>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答