javascript - 事件监听器没有触发?

标签 javascript html dom-events

我正在尝试将从输入收集的数组元素推送到 HTML 表。由于某种原因,事件监听器未触发,这是 HTML。

        <form id="frm1">
      <input id='keyword-input' type="text" placeholder="Keywords"></input>
      <input id="color-input" type="text" placeholder="Color"></input>
      <input id="size-input" type="text" placeholder="Size"></input>
      <input id="profile-input" type="text" placeholder="Profile"></input>
      <input id="proxy-input" type="text" placeholder="Proxy"></input>
      <input id="category-input" type="text" placeholder="Category"></input>
      <input id="tasks-input" type="number" placeholder="# Of Tasks"></input>
      <input id="schedule-input" type="time" placeholder="Schedule Task"></input>
      <input id="search-input" type="text" placeholder="Search Method"></input>
      <button type="submit" form="frm1" class="add-button" id='addTask'>Add Task</button>
    </form>

我尝试将监听器进一步移至脚本下方,并尝试将其嵌入到 onload 函数中,但都没有解决问题。

var submitButton = document.getElementById('addTask');


submitButton.addEventListener('submit', displayTable);


let taskModel = [{
    taskKeyword : value,
    taskSize : value,
    taskProfile : value
}];


function displayTable(taskModel) {
    var table = document.getElementById('taskTable');

    for (var i = 0; i < taskModel.length; ++i) {
      var tasks = tasks[i];

      var row = document.createElement('tr');

      var properties = ['taskKeyword', 'taskSize', 'taskProfile'];

      for (var j = 0; j < properties.length; ++j) {

        var cell = document.createElement('td');


        cell.innerHTML = taskModel[properties[j]];


        row.appendChild(cell);
      }

      table.appendChild(row);
    }
  }

我希望在按下“addTask”按钮后执行该函数,但它没有出现在开发工具事件监听器中。

最佳答案

您必须将监听器添加到您的表单而不是按钮

来自official docs :

The submit event is fired when a form is submitted.

Note that submit is fired only on the form element, not the button or submit input.

Forms are submitted, not buttons.

<小时/>

对代码的重要更改:

  • 第一:displayTable 处理函数中,将参数更改为其他名称,而不是 taskModel

  • 为什么?您尝试使用 taskModel 并假设它保存任务数据。然而,调用该函数时,taskModel 的实际值是event 数据。默认情况下,处理函数在执行时会传递事件对象(当您感兴趣的事件/操作发生时创建的)作为参数。

  • 第二点:taskModel[properties[j]] 更改为 taskModel[0][properties[j]]

  • 为什么?由于您将其声明为数组,因此在访问它时必须指定 taskModel索引

var taskForm = document.getElementById('frm1');

taskForm.addEventListener('submit', displayTable);

function displayTable(event) {
  let taskModel = [{
    taskKeyword: document.getElementById('keyword-input').value,
    taskSize: document.getElementById('size-input').value,
    taskProfile: document.getElementById('profile-input').value
  }];
  var table = document.getElementById('taskTable');

  for (var i = 0; i < taskModel.length; ++i) {
    //var tasks = tasks[i];
    var row = document.createElement('tr');
    var properties = ['taskKeyword', 'taskSize', 'taskProfile'];
    for (var j = 0; j < properties.length; ++j) {
      var cell = document.createElement('td');
      cell.innerHTML = taskModel[0][properties[j]]; // You have to specify the index of the taskModel since you declared it as an array
      row.appendChild(cell);
    }
    table.appendChild(row);
  }

  // added event.preventDefault(); for demo purposes
  event.preventDefault();
}
<form id="frm1">
  <input id='keyword-input' type="text" placeholder="Keywords"></input>
  <input id="color-input" type="text" placeholder="Color"></input>
  <input id="size-input" type="text" placeholder="Size"></input>
  <input id="profile-input" type="text" placeholder="Profile"></input>
  <input id="proxy-input" type="text" placeholder="Proxy"></input>
  <input id="category-input" type="text" placeholder="Category"></input>
  <input id="tasks-input" type="number" placeholder="# Of Tasks"></input>
  <input id="schedule-input" type="time" placeholder="Schedule Task"></input>
  <input id="search-input" type="text" placeholder="Search Method"></input>
  <button type="submit" form="frm1" class="add-button" id='addTask'>Add Task</button>
</form>

<table id="taskTable">
</table>

注意:出于演示目的,我修改了此答案的 taskModel 实现。

关于javascript - 事件监听器没有触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946386/

相关文章:

javascript - Apple 是否发布了用于 touch bar 的 API?

angularjs - 检查 event.target 是否是 Angular 指令(元素)的子元素

javascript - Gulp watch() 不触发 less 进程

javascript - 如何使用dojo debounce和throttle?

javascript - 适用于 Android 的 Meteor 构建 : TypeError: Cannot read property 'slice' of null error when I run the production built server

javascript - Node mysql插入当前日期

javascript - 如何在谷歌地图上添加窗帘

html - Internet Explorer 11 HTML5 音频持续时间 = 无限问题

html - 文本未与顶部对齐

javascript - HTML5 使用 javascript 拖放 DOM 操作