首页 > 解决方案 > 将两个变量合并为一个变量并与另一个变量相等

问题描述

该代码没有任何错误,但不起作用。我填写了两个输入并单击按钮,但它没有打印出单词。

let listItem = document.getElementById('todoListItem');
let timeInputValue = document.getElementById('timeInput');
let getInputValue = document.getElementById('task');
let errorMessageInput = document.getElementById('errorMsg');

function addTask() {
  if (getInputValue.value.trim() === "") {
    errorMessageInput.textContent = "You haven't added a todo item, please add it in order to show up";
    
    return;
  }

  let myTodoInput = listItem.textContent;
  let myTodoTime = timeInputValue.textContent;
  let myTodoInfo = myTodoInput + ' Time ' + myTodoTime;

  myTodoInfo = getInputValue.value;
}
<div class="myApp border border-info">
  <p class="text-center appTittle">To Do List</p>
  <div class="todoThings" id="todo">
    <ul>
      <li id="todoListItem"></li>
    </ul>
  </div>
  <div class="row mx-auto">
    <p class="mr-3 ml-3 appTaskText">Task</p><input type="text" class="appInput" id="task">
    <p class="mr-1 ml-3 appTaskText">Time</p><input type="number" id="timeInput" min="1" max="24">
  </div>
  <button type="submit" class="btn btn-info btn-sm addButton" id="add" onclick="addTask()">Add task</button>
  <p id="errorMsg" class="text-danger mt-1 mb-1"></p>
</div>

标签: javascripthtml

解决方案


我解决了我的问题这是修复

let listItem = document.getElementById('todoListItem');
let timeInputValue = document.getElementById('timeInput');
let getInputValue = document.getElementById('task');
let errorMessageInput = document.getElementById('errorMsg');

function addTask() {
    if (getInputValue.value.trim() === "") {
        errorMessageInput.textContent = "You haven't added a todo item, please add it in order to show up";
        return;
    }

    let myTodoInput = getInputValue.value;
    let myTodoTime = timeInputValue.value;
    let myTodoInfo = myTodoInput + ' at ' + myTodoTime + 'PM';

    listItem.textContent = myTodoInfo;
}

推荐阅读