【问题标题】:Sending Selected button name through Vue / Axios通过 Vue / Axios 发送选定的按钮名称
【发布时间】:2018-12-12 00:08:39
【问题描述】:

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

更新

<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 通过表单推送单击按钮的 BUTTON TEXT 部分(总共 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)
            });
        }
    }
});

任何帮助将不胜感激。

谢谢,杰克。

【问题讨论】:

  • @hasClicked() 是什么?
  • @EmileBergeron 这只是为了测试,我已经从上面的代码中删除了。

标签: php vue.js vuejs2 axios


【解决方案1】:

如果需要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>

【讨论】:

  • 我收到未捕获的类型错误:无法读取未定义的属性“innerHTML”。另外,对于发布所选按钮文本值的多个按钮,这将如何工作?谢谢
  • 您是否将ref 属性添加到按钮中,如示例所示?请参阅我的更新答案。
  • 嘿,我已经更新了我的代码,但我仍然无法通过 Axios 获取按钮文本
  • 好的,你有 3 个按钮,默认是 SUBMIT 类型。您可以阻止默认操作(即提交表单),并阻止单击事件传播到父元素。所以counter 包含最后点击按钮的值。您还需要一个变量,例如lastButton 存储button 对象。然后在submit() 方法中,你将从lastButton 对象中获得你需要的东西——据说是fromto 键。
猜你喜欢
  • 2021-10-05
  • 2018-12-12
  • 1970-01-01
  • 2021-05-22
  • 2019-07-10
  • 2015-08-23
  • 2012-09-01
  • 2019-08-28
  • 2019-03-22
相关资源
最近更新 更多