【问题标题】:Javascript return confirm() when input name=confirm gives confirm is not a function当输入名称=确认给出确认不是函数时,Javascript返回确认()
【发布时间】:2021-04-13 21:12:09
【问题描述】:

我的代码在控制台中显示此错误:

未捕获的类型错误:确认不是函数

我可以通过将输入重命名为“确认”以外的名称来解决它。
为什么命名输入会与 JavaScript 的确认函数冲突?

<form action="" method="POST">
  <input type="text" name="confirm">
  <input type="submit" onclick="return confirm('Text')">
</form>

【问题讨论】:

  • "为什么命名输入会与 javascripts 确认函数冲突?" - 因为在 1997 年,Netscape 或 Microsoft 的某个人认为这将是一个好主意。
  • 改用window.confirm

标签: javascript html input confirm


【解决方案1】:

在 JavaScript 中,输入元素的名称成为关联表单对象的属性。在您的情况下,名为“confirm”的输入成为其形式的属性,shadows confirm method 继承自 window object

window.confirm => form.confirm
&lt;input type="text" name="confirm"&gt; => form.confirm

作为上面cmets中的mentioned by Dai,可以使用window.confirm()保证访问window对象的confirm方法。

<form action="" method="POST">
  <input type="text" name="confirm">
  <input type="submit" onclick="return window.confirm('Text')">
</form>

或者,如您的问题中所述,使用不同的输入名称:

<form action="" method="POST">
  <input type="text" name="confirmation">
  <input type="submit" onclick="return confirm('Text')">
</form>

更多参考,另见:

HTMLFormElement

警告:避免为表单元素指定与表单的内置属性相对应的名称,因为这样您将使用对相应输入的引用来覆盖预定义的属性或方法。

input name called submit
confirm is not a function
confirm not working

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多