javascript - vuejs 如何更改组件的输入值

标签 javascript vuejs2 vue-component buttonclick

我有一个带有隐藏输入的表单。我的数据有一个小列表。只有 titleid。该列表由 vue 组件创建。我想单击此列表项,然后更改为隐藏的输入值。这是我的结构。

HTML

<div id="v-account-select">
  <form method="post">
    {!! csrf_field() !!}
    <input type="hidden" name="id" v-model="account_id">
  </form>
  <account-select :datalist="{{ json_encode($list) }}"></account-select>
</div>

APP.JS

Vue.component("account-select", {
  datalist: {
    type: Array,
    required: true,
  },
  methods: {
    item_id_bind(id) {
      this.$emit("#account_id", id);
    },
  },
  template:
    '<table class="table table-responsive table-striped table-bordered">' +
    "<tbody>" +
    '<tr v-for="item in datalist"><td>' +
    '<button type="button" class="btn-link" v-on:click="item_id_bind(item.id)">{{item.title}}</button>' +
    "</td></tr>" +
    "</tbody>" +
    "</table>",
});

这是我的代码。

最佳答案

添加事件处理程序。

<account-select @account-change="onAccountChange" :datalist="{{ json_encode($list) }}"></account-select>

在你的父 Vue 中添加

methods:{
    onAccountChange(id){
        this.account_id = id
    }
}

并将您的组件更新为

methods: {
    item_id_bind(id) {
        this.$emit("account_change", id)
    }
},

这是一个工作示例。

console.clear()

Vue.component('account-select', {
  props: {
    datalist: {
      type: Array,
      required: true
    }
  },
  methods: {
    item_id_bind(id) {
      this.$emit("account-change", id)
    }
  },
  template:`
    <table class="table table-responsive table-striped table-bordered">
      <tbody>
        <tr v-for="item in datalist" :key="item.id">
          <td>
            <button type="button" 
                    class="btn-link" 
                    v-on:click="item_id_bind(item.id)">
              {{item.title}}
            </button>
          </td>
        </tr>
      </tbody>
    </table>
  `
});


new Vue({
  el: "#app",
  data: {
    datalist: [{
      id: 1,
      title: "item1"
    }, {
      id: 2,
      title: "item2"
    }],
    account_id: null
  },
  methods: {
    onAccountChange(id) {
      this.account_id = id
    }
  }
})
<script src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script>
<div id="app">
  <div id="v-account-select">

    <form method="post">
      Current Account id: {{account_id}}
      <input type="hidden" name="id" v-model="account_id">
    </form>
    <account-select @account-change="onAccountChange" :datalist="datalist"></account-select>
  </div>
</div>

关于javascript - vuejs 如何更改组件的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44574149/

相关文章:

typescript - 如何使用 TypeScript 在 Vue.js 中使用 Provide/Inject

javascript - 带有复选框和搜索方法的多选下拉菜单

javascript - setInterval 和 promises 产生一个 PromiseRejectionHandledWarning

javascript - 使用递增变量时 jquery 循环不起作用

vue.js - NuxtJS/VueJS : How to know if page was rendered on client-side only?

module - 无模板的组件

javascript - 从表单收集数据,堆叠在对象中并使用 Vue 通过 AJAX 发送

typescript - 为什么即使我能够获得对我尝试滚动到的元素的引用,scrollIntoView 在 Vue 3 中不起作用

javascript - 为什么在父子之间传递的 Prop 数据在 VueJS 中不起作用?

javascript - Intellij 找不到 .js 文件(类搜索)