簡體   English   中英

如何從 html 中的 id 字段中提取 id 以通過 Vue 中的 axios 發送?

[英]How to extract an id from id field in html to send it via axios in Vue?

我不知道如何獲取元素的 id 以便發送它。 我使用 Vue 2。

** 提供我從 API 獲得的數組,所以這只是一個示例。

我需要動態獲取 id 以便為確切的報價發送評論。

也許這是錯誤的方法,但如果它以這種方式工作會很棒

HTML:

<div v-for="offer in offers">
        <p> {{ offer.description }}</p>

      <div class="form-group col-md-9">
        <b-form-textarea :id="`${offer.id}`" placeholder="Comment!" rows="1" v-model="text"></b-form-textarea>
        <button @click="createComment()" class="button btn btn-success">Send!</button>
      </div>

</div>

JS:

data () {
      return {
          offers: [
    {id: 1,
     description: "some"
    },
    {id: 2,
     description: "another"
    },
],
        }
    },


        methods: { 
  createComment() {
     const FormData = { 
      text: this.text,
      offer_related:   NEED TO GET THE VALUE OF TEXTAREA :id here
    } 
    // axios
    // .post(`/api/v1/comments/`, FormData)
  },


非常感謝@don_aman!

最終代碼將如下所示:

<div v-for="offer in offers">
        <p> {{ offer.description }}</p>

      <div class="form-group col-md-9">
        <b-form-textarea :id="`${offer.id}`" placeholder="Comment!" rows="1" v-model="text[offer.id]"></b-form-textarea>
        <button @click="createComment(`${offer.id}`)" class="button btn btn-success">Send!</button>
      </div>

</div>

JS:

data () {
      return {
          offers: [
    {id: 1,
     description: "some"
    },
    {id: 2,
     description: "another"
    },
],
     text: {},
        }
    },


        methods: { 
  createComment(id) {
     const FormData = { 
      text: this.text[id],
      offer_related:id,
    } 
    axios
    .post(`/api/v1/comments/`, FormData)
  },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM