通过Vue / Axios发送选定的按钮名称

时间:2018-07-03 15:48:37

标签: php vue.js vuejs2 axios

我在一个如下所示的文件中有一个按钮

已更新

<div v-for="button in buttons"
    :key="button.value"
    class="gc gc-one-third gc-whole-to2">
    <button @click.stop.prevent="onClick(button)">{{ button.from }} <br /> {{ button.to }}</button>
</div>

如何使用Axios和Vue在表单中按一下按钮的“按钮文本”部分(总共3个),因为它说我不能使用v-model。

已更新

const app = new Vue({
    el: '#app',
    data() {
        return {
            step: 1,
            counter: 0,
            buttons: [{
                value: 1,
                from: '£3,000',
                to: 'TO £5,000'
            }, {
                value: 2,
                from: '£5,000',
                to: 'TO £10,000'
            }, {
                value: 3,
                from: 'OVER',
                to: '£10,0000'
            }],
            debt: {
                name: null,
                email: null,
                tel: null
            }
        }
    },
    methods: {
        prev() {
            this.step--;
        },
        next() {
            this.step++;
        },
        onClick(button) {
            this.counter = button.value;
        },
        submit() {
            axios.post('post.php', {
                'name': this.debt.name,
                'email': this.debt.email,
                'tel': this.debt.tel,
            }).then(response => {
                console.log('success', response.data.message)
            }).catch(error => {
                console.log(error.response)
            });
        }
    }
});

任何帮助将不胜感激。

谢谢,杰克。

1 个答案:

答案 0 :(得分:0)

如果您需要counter

的值
    submit() {
        axios.post('post.php', {
            'name': this.debt.name,
            'email': this.debt.email,
            counter: this.counter
        }).then(response => {
            console.log('success', response.data.message)
        }).catch(error => {
            console.log(error.response)
        });
    }

如果您需要按钮的标题

<button ref="myBtn" @click.stop.prevent="counter = 1">VALUE</button>

    submit() {
        axios.post('post.php', {
            'name': this.debt.name,
            'email': this.debt.email,
             value: this.$refs.myBtn.innerHTML
        }).then(response => {
            console.log('success', response.data.message)
        }).catch(error => {
            console.log(error.response)
        });
    }

更新

如果您有一个包含10个按钮的表单,并希望发布使用AXIOS单击的按钮的标题,则:

<form>
  <div>
    some text <button type="button" @click="submit">VALUE</button>
  </div>
  <div>
    some other text <button type="button" @click="submit">OTHER VALUE</button>
  </div>
  <div>
    something else <button type="button" @click="submit">EVEN OTHER</button>
  </div>
</form>


<script>
submit(event) {
    axios.post('post.php', {
        'name': this.debt.name,
        'email': this.debt.email,
         value: event.target ? event.target.innerHTML : ""
    }).then(response => {
        console.log('success', response.data.message)
    }).catch(error => {
        console.log(error.response)
    });
}
</script>