数组始终选择第一个值而不是特定值

时间:2016-10-25 20:03:22

标签: javascript jquery arrays

refreshFileList = function() {
  $("#filedetails tr").remove();
  for (i = 0, j = fileDetails.length; i < j; ++i) {
    $("#filedetails").append("<tr data-filesize='" + fileDetails[i].SIZE + "'  data-filename='" + fileDetails[i].KEY + "'><td><strong>" + fileDetails[i].FILENAME + "</strong></td><td class='nodesize'>" + fileDetails[i].SIZE + " MB</td><td>" + fileDetails[i].EXT + "</td>" + fileDetails[i].TAG + "</tr>");
  }
},

fileDelete = function(e) {
  e.preventDefault();
  var parentRow = jQuery(this).closest('tr')
    , fileName = fileDetails[i].KEY
    , fileSize = fileDetails[i].SIZE;
    ajaxFileDelete(fileName, parentRow, fileSize);
},

进入fileDelete函数我不想使用data-filename和data-filesize但是当我要使用fileName = fileDetails [i] .KEY或fileSize = fileDetails [i] .SIZE时它总是删除第一个数组的值而不是特定的值,但数据属性按预期工作。

1 个答案:

答案 0 :(得分:1)

i添加到<tr>作为data属性。

refreshFileList = function() {
  $("#filedetails tr").remove();
  for (var i = 0, j = fileDetails.length; i < j; ++i) {
    $("#filedetails").append("<tr data-index='" + i + "'><td><strong>" + fileDetails[i].FILENAME + "</strong></td><td class='nodesize'>" + fileDetails[i].SIZE + " MB</td><td>" + fileDetails[i].EXT + "</td>" + fileDetails[i].TAG + "</tr>");
  }
},

fileDelete = function(e) {
  e.preventDefault();
  var parentRow = jQuery(this).closest('tr')
    , i = parentRow.data('index')
    , fileName = fileDetails[i].KEY
    , fileSize = fileDetails[i].SIZE;
    ajaxFileDelete(fileName, parentRow, fileSize);
},