【问题标题】:How to get the index of v-for in a function in vue js html?如何在vue js html中的函数中获取v-for的索引?
【发布时间】:2018-11-16 00:12:47
【问题描述】:

如果状态为真,我需要从 html vue js 中的字段拼接该数组

我需要将索引值传递给函数。 怎么可能。

我的错误是

Uncaught ReferenceError: index is not defined
    at Object.success (80155f2d-c534-4b8f-ba5f-20edef1496ab:1032)
    at i (jquery-3.2.1.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2)
    at A (jquery-3.2.1.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4)

我的html代码是

<div class="card-content" v-for="(arrest, index) in arestee" :key="index">
  --my data--
 <a @click="deleteSubmit(arrest)" target="_blank">Delete Arestee</a></button>

 </div>

单击删除提交逮捕后,我将数据传递给函数

我的vue js代码是

 deleteSubmit: function(arrest) {
                                    var vm = this;
                                    data = {};
                                    data['docId'] = this.id;

                                    $.ajax({
                                        url: 'http://localhost:4000/doc/remove/',
                                        data: data,
                                        type: "POST",
                                        dataType: 'json',
                                        success: function(arrest) {
                                            if (arrest.status)
                                            {

                                                alert("Success")
                                                arrest.splice(index, 1);

                                            }
                                            else {
                                                alert("Failed")


                                            }
                                        }
                                    });
                                    return false;
                                },



arrest.splice(index, 1);

我需要将索引值传递给这个

【问题讨论】:

  • 也只是通过了索引,就像你通过逮捕一样。像这样:deleteSubmit(arrest, index)
  • 如果有,如何更新功能?

标签: javascript html vue.js vue-component


【解决方案1】:

首先,正如 Vamsi Krishna 评论的那样,您必须将 index 传递给 deleteSubmit

 <a @click="deleteSubmit(arrest, index)" target="_blank">Delete Arestee</a></button>

然后你可以bind回调中的索引:

deleteSubmit: function(arrest, index) {
  ...
  success: function(index, arrest) {
    if (arrest.status){
      alert("Success")
      arrest.splice(index, 1);
    } else {
      alert("Failed")
    }
  }.bind(null, index)
  ...
}

【讨论】:

  • 未捕获的类型错误:在 Object.fireWith 的 i (jquery-3.2.1.min.js:2) [如 resolveWith] (jquery-3.2.1.min.js:2) 在 A (jquery-3.2.1.min.js:4) 在 XMLHttpRequest. (jquery-3.2.1.min.js:4)
  • 这可能是您的arrest.splice 中的错误,请与console.log(index, arrest) 核对,并检查值是否已传递并在您的成功回调中保持正确的值。
  • 是的,console.log(索引,逮捕)的值正确
  • 看来arrest 不是数组类型?
  • 在 if(arrest.status) 中,如果我输入 console.log(arrest,index) 索引是正确的但逮捕是状态:true
猜你喜欢
  • 2018-09-15
  • 2020-10-25
  • 1970-01-01
  • 2022-11-13
  • 2021-04-15
  • 2018-09-16
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多