【问题标题】:Laravel 5 : How to get the value in data attribute using vue jsLaravel 5:如何使用 vue js 获取数据属性中的值
【发布时间】:2020-02-17 03:44:06
【问题描述】:

我是 vue js 的新手。在我们的应用程序中,我们验证该值是否已存在于数据库中。我想通过使其成为动态来改进它。因此,每当用户键入任何内容时,我都会将数据属性添加到我的字段中。我在数据属性中的值是我将检查该值是否已存在的表。

Add.vue

<label>Code <span class="required-field">*</span></label>
       <input type="text" name="code" @keyup="checkCOACode" v-model="coa_code" class="form-control" :data-table="chart_of_accounts">

在我的方法中添加.vue

checkCOACode(e) {
    e.preventDefault();
    var code = this.coa_code;
    var x    = event.target.getAttribute('data-table');

    alert(x);
    return false;
    axios.post("/checkIfCodeExists", {code:code})
        .then((response)  =>  {
            var code_checker = '';
            if (response.data == 0) {
                $('.add-chart-of-account').removeAttr('disabled','disabled');

            }else{
                $('.add-chart-of-account').attr('disabled','disabled');
                code_checker    =   'Code is already exist';
            }
            this.coa_checker_result = code_checker;
        });
},

我在 x 中的值为 null。

问题:如何获取数据属性的值?

【问题讨论】:

  • event.target.getAttribute('data-table'); 中的event 是什么?不应该是e.target 吗?

标签: php laravel vue.js laravel-5


【解决方案1】:

您可以通过将ref 属性添加到您的文本元素来获取vuedata 属性的值

<input type="text" ref="coaCode" name="code" @keyup="checkCOACode" v-model="coa_code" class="form-control" :data-table="chart_of_accounts">

然后像这样得到那个属性

checkCOACode(e) {
    e.preventDefault();
    const coa = this.$refs.coaCode
    const coaCode = coa.dataset.table
    alert(coaCode);
    return false;

},

【讨论】:

  • 给我一个未定义的
  • chart_of_accounts" 未在实例上定义,但在渲染期间引用
  • 那么你必须在你的data(){ return { chart_of_accounts: 'your value' } }方法中添加chart_of_accounts
  • IS chart_of_accounts 是您的动态值还是修复字符串?
  • 这是一个修复字符串。那是我的桌子名。
猜你喜欢
  • 2019-10-19
  • 2020-02-17
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 2019-07-30
  • 2020-10-02
  • 2018-08-18
  • 2016-09-22
相关资源
最近更新 更多