简体   繁体   中英

Enable the input when click add new and Disable the input after click the create button

I have an <input> and two <button> s. If I click Add new input , the <input> shows. However, I want to make the <input> disabled after the Create button is clicked. The <input> is working already, but it doesn't get disabled after clicking the Create button.

App.vue

<template>
  <div id="app">
    <button class="button is-orange has-text-white" @click="addRow">Add Input</button>
    <table>
      <thead>
        <tr>
          <th></th>
        </tr>
      </thead>

      <tbody>
        <tr v-for="(input, index) in inputs">
          <td width="1%">{{++index}}</td>
          <td width="18%">
            <b-field>
              <input v-model="input.one" id="one" class="input one" type="text" placeholder>
            </b-field>
          </td>
          <td width="15%">
            <button class="button is-orange has-text-white" @click="inserts(inputs)">Create</button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  name: "App",
  components: {},
  data() {
    return {
      searchorderid: null,
      inputs: [],
      data: {},
      loading: false
    };
  },
  methods: {
    addRow() {
      this.inputs.push({
        one: ""
      });
    },
    deleteRow(index) {
      this.inputs.splice(index, 1);
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

I want the output to look like this:

期望的结果

After Create is clicked, the preceding <input> should be disabled.

demo

You could update inserts() to:

  1. Receive the input iterator item (instead of inputs , which is already accessible via this.inputs ):

     <tr v-for="(input, index) in inputs"> <.-- BEFORE --> <.-- <button @click="inserts(inputs)"> --> <button @click="inserts(input)">... </button> </tr>
  2. Add a Boolean prop to the input iterator item that indicates whether the corresponding <input> should be disabled:

     export default { methods: { // BEFORE: //inserts(inputs) { inserts(input) { this.$set(input, 'disabled', true) } } }
  3. Bind <input> 's disabled prop to the newly added prop from the previous step:

     <tr v-for="(input, index) in inputs"> <input:disabled="input.disabled">... </tr>

编辑在每个项目的 v-for 中禁用输入

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM