【问题标题】:onchange function isn't defined when I use string templates使用字符串模板时未定义 onchange 函数
【发布时间】:2021-07-17 17:53:43
【问题描述】:

我正在尝试创建一个在输入值更改时调用的函数。单击按钮时会创建输入。我正在尝试使用onchange 属性作为输入,但是当我编辑第一个输入字段的值时,我收到此错误:addTitle is not defined at HTMLInputElement.onchange。我不明白为什么没有定义函数。

HTML:

<div id="self-container">
            <fieldset class="self">
                <legend class="self-title">curently reading</legend>
                <button class="add-book-btn"><i class="fas fa-plus plus-icon"></i></button>
            </fieldset>
            <fieldset class="self">
                <legend class="self-title">want to read</legend>
                <button class="add-book-btn"><i class="fas fa-plus plus-icon"></i></button>
            </fieldset>
            <fieldset class="self">
                <legend class="self-title">read</legend>
                <button class="add-book-btn"><i class="fas fa-plus plus-icon"></i></button>
            </fieldset>
        </div>

JS:

const selfs = document.getElementsByClassName("self");
const addBookBtn = document.getElementsByClassName('add-book-btn');

let myBooks = [];

function createBook(title, author, rating) {
    let book = {};
    book.title = title;
    book.author = author;
    book.rating = rating;

    return book;
}

function addTitle(title) {
    console.log("The title is " + title);
}

for (let i = 0; i < addBookBtn.length; i++) {
    
    

    function addBookCard() {
            addBookBtn[i].addEventListener('click', function () {
                myBooks.push(createBook());
                selfs[i].innerHTML += `
                <div class="book-card bc-display">
                    <input type="text" placeholder="title" onchange="addTitle(this.value)">
                    <input type="text" placeholder="author">
                    <input type="text" placeholder="rating">
                </div>
                `;
                addBookCard();
            });
        };
    addBookCard();
}

【问题讨论】:

  • 为什么你在loop里面定义了函数?
  • @BadPiggie 首先我在没有循环的情况下尝试了它,但由于某种原因,第一次单击后 + 按钮不起作用。所以,我不得不使用循环来重新定义他的 eventListener
  • 你不应该在循环内定义函数。
  • @BadPiggie 那我该怎么办?所以按钮仍然有效。 stackoverflow.com/questions/68416595/…

标签: javascript html


【解决方案1】:

这是实现此目的的一种更简洁的方法:

HTML 代码:

<div id="self-container">
  <fieldset class="self">
    <legend class="self-title">curently reading</legend>
    <button class="add-book-btn"><i class="fas fa-plus plus-icon"></i></button>
  </fieldset>
  <fieldset class="self">
    <legend class="self-title">want to read</legend>
    <button class="add-book-btn"><i class="fas fa-plus plus-icon"></i></button>
  </fieldset>
  <fieldset class="self">
    <legend class="self-title">read</legend>
    <button class="add-book-btn"><i class="fas fa-plus plus-icon"></i></button>
  </fieldset>
</div>

JS 代码:

const selfs = document.getElementsByClassName("self");
const addBookBtn = document.getElementsByClassName('add-book-btn');

let myBooks = [];

function createBook(title, author, rating) {
  let book = {};
  book.title = title;
  book.author = author;
  book.rating = rating;

  return book;
}

function addTitle(title) {
  console.log("The title is " + title);
}

function onButtonClicked(index) {
  let html_string = `
                <div class="book-card bc-display">
                    <input type="text" placeholder="title" onchange="addTitle(this.value)">
                    <input type="text" placeholder="author">
                    <input type="text" placeholder="rating">
                </div>
                `;
  selfs[index].insertAdjacentHTML("beforeend", html_string);
};

for (let i = 0; i < addBookBtn.length; i++) {
  addBookBtn[i].addEventListener('click', function() {
    onButtonClicked(i)
  }, false);
};

现场演示: https://jsfiddle.net/h1a92jg7/1/

【讨论】:

  • 它在 jsfiddle 上运行良好,但由于某种原因,当我从 VS 代码运行它时仍然出现错误
  • 我有 `type="module"` 作为script 属性。我删除了,现在它可以工作了
猜你喜欢
  • 2010-10-11
  • 2012-09-19
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多