【问题标题】:Post two field values with axios using a bootstrap vue b-form-select component使用引导程序 vue b-form-select 组件使用 axios 发布两个字段值
【发布时间】:2020-12-16 09:28:03
【问题描述】:

我正在尝试将两个字段发布到我的后端,但是当我尝试时,后端的两个值都为 null。

不知道我在这里做错了什么。

<template>
  <div id="app">
    <div>
      <b-form-select
        v-model="sport_id_form"
        :options="sport_id_form_options"
        class="mb-3"
        value-field="item"
        text-field="name"
      ></b-form-select>
      <div class="mt-3">
        :Selected
        <strong>{{sport_id_form}}</strong>
      </div>
      <button type="button" class="btn btn-dark" v-on:click="get_sports">Get Sports</button>
    </div>
  </div>
</template>


<script>
export default {
  middleware: ["auth"],
  components: {},
  data() {
    return {
      sport_id_form: [],
      sport_id_form_options: [
        { item: null, name: "Please select an option" },
        { item: { "9": "Tennis" }, name: "Tennis" },
        { item: { "10": "Basketball" }, name: "Basketball" }
      ]
    };
  },
  methods: {
    async get_sports() {
      if (this.errors.any()) {
        return;
      }
      await this.$axios.post(
        "api/sportsid/add_sports/",
        this.sport_id_form + "/" + this.sport_id_form_options
      );
      this.$router.push("/");
    }
  }
};
</script>

如果我以这种方式创建表单,一切正常。

<script>
export default {
  middleware: ["auth"],
  data() {
    return {
      form: {
        sport_id: "",
        sport_name: ""
      }
    };
  },
  methods: {
    async submit() {
      if (this.errors.any()) {
        return;
      }
      // check valid etc....
      await this.$axios.post("/api/sportsid/add_sports/", this.form);
      this.$router.push("/");
    }
  }
};
</script>

当我使用表单时,数据会进入后端并根据值触发事件。

如何以与表单相同的格式发布数据但使用下拉选择?

【问题讨论】:

    标签: javascript vue.js bootstrap-4 nuxt.js


    【解决方案1】:

    改变表格

    data() {
        return {
          sport_id_form: {
            sport_id: "",
            sport_name: ""
          },
          sport_id_form_options: [
            { item: null, name: "Please select an option" },
            { item: { "9": "Tennis" }, name: "Tennis" },
            { item: { "10": "Basketball" }, name: "Basketball" }
          ]
        };
      },
    

    然后选择

    <b-form-select
            v-model="sport_id_form.sport_id"
            :options="sport_id_form_options"
            class="mb-3"
            value-field="item"
            text-field="name"
          ></b-form-select>
    

    【讨论】:

    • 我不明白你。表单数据示例有效,但我的下拉示例无法显示?我在 axios 示例中看不到
    • 您可以在网络标签中使用请求检查表单数据
    • 我的回答仍然是空值。不知道为什么。内容好像是空的。
    • 安装vue devtools并检查更改表单时值是否更新,
    • 是的,我可以在开发工具中看到值更新。问题是发布请求。我选择的数组的值为 {"9" : "Tennis") 我可以看到这一点,但我无法拆分键和值以进入 url 以进行发布。你知道怎么做吗?
    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2020-04-23
    • 2020-12-04
    • 2021-01-09
    • 2023-03-08
    • 2020-04-19
    相关资源
    最近更新 更多