我无法清除两个输入之一

发布于 2025-01-13 04:35:40 字数 548 浏览 1 评论 0原文

我无法清除名称输入 当我进入严格模式控制台时说:“无法在字符串''上创建属性'值'”

    const number = document.getElementById("number").value;
    const name = document.getElementById("name").value;

    console.log(name)
    console.log(number)

    if (name.trim() && number.trim()) {
        let contact = {
            name,
            number
        };
        renderContact(contact);
        console.log(contacts);
        contacts.push(contact)
    }
    // clearInput(number, name);
    this.name.value = '';
    this.number.value = '';
};

i cant clear name input
when i go into strict mode console say: "Cannot create property 'value' on string ''"

    const number = document.getElementById("number").value;
    const name = document.getElementById("name").value;

    console.log(name)
    console.log(number)

    if (name.trim() && number.trim()) {
        let contact = {
            name,
            number
        };
        renderContact(contact);
        console.log(contacts);
        contacts.push(contact)
    }
    // clearInput(number, name);
    this.name.value = '';
    this.number.value = '';
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无言温柔 2025-01-20 04:35:40

你的“名字”和“号码”不是 DOM 元素。他们的价值观是:“你好,世界”等等。 String 是原始类型,不能在严格模式下在原型中定义属性。

const numberInput = document.getElementById("number")
const nameInput = document.getElementById("name")

const name = numberInput.value
const number = numberInput.value

if (name.trim() && number.trim()) {
    let contact = {
        name,
        number
    };
    renderContact(contact);
    console.log(contacts);
    contacts.push(contact)
}
// clearInput(numberInput, nameInput);
nameInput.value = '';
numberInput.value = '';

};

Your "name" and "number" is not DOM elements. There's their values like: "Hello, World" and other. String is primitive type and you can't define properties in prototype in strict mode.

const numberInput = document.getElementById("number")
const nameInput = document.getElementById("name")

const name = numberInput.value
const number = numberInput.value

if (name.trim() && number.trim()) {
    let contact = {
        name,
        number
    };
    renderContact(contact);
    console.log(contacts);
    contacts.push(contact)
}
// clearInput(numberInput, nameInput);
nameInput.value = '';
numberInput.value = '';

};

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文