开发者问题收集

Javascript 未捕获的类型错误:value.toUpperCase 不是一个函数

2020-08-30
457

我尝试使用以下脚本让用户根据他们输入的值过滤表中的行。他们可以更新该行,更新事件发生后,页面会刷新/重新加载,页面会再次显示所有行。我尝试找到一种方法来保留刷新/重新加载后他们过滤的行。换句话说,好像从未发生过刷新一样。

I get the following error: Uncaught TypeError: value.toUpperCase is not a function
    at filterTable (accounts.php:1270)
    at HTMLInputElement.<anonymous> (accounts.php:1291)
    at HTMLInputElement.<anonymous>

这是代码;

  <table class="userprof" align='left'>
    <tr>
      <td class="footer">Filter:
        <input type="text" id="myInput" name="filter" style="color:black !important;" placeholder="Filter table" />
      </td>
    </tr>
  </table>
</p><br /><br /><br />
    

    
</head>


    <table width="99%" id="myTable" class="sortable" >
    <tr>
 
    <td class="header" style="padding: 1px;">Name</td>
    <td class="header">Email</td></tr>
 
 
<?  //Selecteer accounts.
  

  $stmt = $mysqli->prepare("SELECT * FROM account WHERE purpose= ?");
  $stmt->bind_param("s", $status);
  $stmt->execute();
  $result = $stmt->get_result(); 
}
 while ($row = $result->fetch_assoc()) { 

  
    <td style="color:<?= htmlspecialchars($cat_color) ?> ", class="footer"><?= htmlspecialchars($row['name']) ?></td>
    

    <td style="color:<?= htmlspecialchars($cat_color) ?> ", class="footer"><?= htmlspecialchars($row['email']) ?></td>
  
    
    <td class="footer" width="1px"><input type="button" value="Edit" id="<?php echo htmlspecialchars($row['id']); ?>" onClick="this.style.color='gold';"
    class="submit edit_data" /></td>
  
   
    </form>
    </tr>
    
    
    <? } ?>
    </table><div id="bottom"></div>

    <script type="text/javascript">
// Store the input in a variable for reference.
var myInput = document.getElementById("myInput");
var savedValue = getSavedValue("myInput");

// Immediately filter the table and set the input value.
filterTable(savedValue);
myInput.value = savedValue;

//Save the value function - save it to localStorage as (ID, VALUE)
function saveValue(e) {
  var id = e.id; // get the sender's id to save it . 
  var val = e.value; // get the value. 
  localStorage.setItem(id, val); // Every time user writing something, the localStorage's value will override . 
}

//get the saved value function - return the value of "v" from localStorage. 
function getSavedValue(v) {
  if (!localStorage.getItem(v)) {
    return ""; // You can change this to your default value. 
  }
  return localStorage.getItem(v);
}

function filterTable(value) {
  console.log(value);
  var filter = value.toUpperCase();
  var rows = document.querySelector("#myTable tbody").rows;

  for (var i = 0; i < rows.length; i++) {
    var nameCol = rows[i].cells[1].textContent.toUpperCase();
    var rankCol = rows[i].cells[2].textContent.toUpperCase();
    var rankerCol = rows[i].cells[5].textContent.toUpperCase();
    var typeCol = rows[i].cells[6].textContent.toUpperCase();
    var emailCol = rows[i].cells[3].textContent.toUpperCase();
    if (nameCol.indexOf(filter) > -1 || rankCol.indexOf(filter) > -1 || rankerCol.indexOf(filter) > -1 || typeCol.indexOf(filter) > -1 || emailCol.indexOf(filter) > -1) {
      rows[i].style.display = "";
    } else {
      rows[i].style.display = "none";
    }
  }
}

myInput.addEventListener('keyup', function(event) {
  console.log(event); // Check if the event is fired.
  var value = event.target;
  saveValue(event);
  filterTable(value);
});

</script>
1个回答
myInput.addEventListener('keyup', function(event) {
 console.log(event); // Check if the event is fired.
 var value = event.target.value;
 saveValue(event);
 filterTable(value);
});

应该是 event.target.value。您错过了 value 属性

Jonathan Akwetey Okine
2020-08-30