如何为提交按钮添加事件监听器

2024-02-25

我在这个 html 上遇到了很多麻烦。我正在尝试向提交按钮添加事件侦听器,以便最终可以更改文档以显示表单信息。问题是,当填写表单时,按钮侦听器不执行任何操作! (它可以在 jsfiddle 和其他类似的东西中工作,但不能作为独立文件工作,这让我相信我以某种方式错误地设置了我的文件,可能弄乱了脚本标签)。我尝试了很多事情,包括移动脚本标签、尝试让事件侦听器提交表单以及输入类型按钮,但仍然一无所获。有人可以帮忙吗?

我的HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Form Project</title>
  <style type="text/css" rel="stylesheet">
    #but{text-align:center;}
    td{text-align:right;}
    span{padding=0; margin=0;float:left;}
  </style>
</head>
<body>
 <form id="formId">
  <table border = "1">

    <tr>
      <th>Provide your contact information</th>
      <th>Provide your login access information</th>
    </tr>


    <tr>
      <td><label><span>First Name:</span> <input type = "text" placeholder = "Enter First Name" required/></label></td>
      <td><label><span>Login ID:</span> <input type = "text" placeholder = "type a login ID" required/></label>  </td>
    </tr>


    <tr>
      <td><label><span>Middle Name:</span> <input type="text" placeholder ="type your middle name"/></label></td>
      <td><label><span>Password:</span> <input type="password" placeholder ="password" required/></label></td>  
    </tr>


    <tr>
      <td><label><span>Last Name:</span> <input type="text" placeholder="Last Name" required/></label></td>
      <td id="but"><label><button type="submit" id="displayButton">Display Info</button></label></td>
    </tr> 


    <tr>
      <td><label><span>Street Address:</span> <input type="text" placeholder="address" required/></label></td>
    </tr>


    <tr>
      <td><label for ="Citylist"><span>City:</span>
        <input type = "text" id ="citylist"
        placeholder="Select a city" list = "cities" required/>
        <datalist id= "cities">
          <option value = "Bronx"/>
          <option value = "Brooklyn"/>
          <option value = "Queens"/>
          <option value = "Manahttan"/>
          <option value = "Staten Island"/>
        </datalist>
        </label>
        </td>
    </tr>

    <tr>
    <td><label for ="StateList"><span>State:</span>
        <input type = "text" id ="State"
        placeholder="Select a city" list = "states" required/>
        <datalist id= "states">
          <option value = "New York"/>
          <option value = "New Jersey"/>
          <option value = "California"/>
          <option value = "Virginia"/>
          <option value = "Maine"/>
        </datalist>
        </td>
    </tr>


    <tr>
      <td><label><span>Zip code:</span> <input type="text" placeholder="Type your zipcode" maxlength="5" required/></label></td>
      </tr>

    <tr>
      <td>
        <label><span>Phone</span>
          <input type ="tel" placeholder="(123)456-789"
          pattern="\(\{3}) +\d{3}-\d{4}" required/>
        </label>
      </td>
    </tr>

    <tr>
      <td>
        <label><span>Email:</span>
          <input type="email" placeholder="[email protected] /cdn-cgi/l/email-protection" required/> 
        </label>
      </td>
    </tr>

    <tr>
      <td>
        <label><span>Birth Date:</span>
          <input type="date" required/>
        </label>
      </td>
    </tr>
  </table>
 </form>
 <script type="text/javascript" src="form.js"></script>

</body>
</html>

我的 JS,仅针对事件侦听器进行了简化:

var button = document.getElementById("displayButton");
//var form = document.getElementById("formId");
//form.addEventListener("submit",function(){console.log("something1"); return false;},false);
button.addEventListener("click", function(){console.log("something"); return false;},false);

function formDisplay(){
    console.log("check");
}

当整个表单未填写时,它会起作用,但如果所有必填字段都已填写,则控制台不会显示“某些内容”。


我建议使用submit事件来处理用户无需单击提交按钮即可提交表单的情况。

正如其他人所说,你需要使用evt.preventDefault()停止提交表单。

下面的示例将在允许提交表单之前检查所有内容是否有效。

var form = document.getElementById("formId");
form.addEventListener("submit", function(evt) {
  for(var i = 0; i < form.elements.length; i++) {
    var el = form.elements[i];
    if (!el.checkValidity()) {
      evt.preventDefault();
      console.log('Fix the form!');
      return;
    }
  }
});
#but {
  text-align:center;
}

td {
  text-align:right;
}

span {
  float:left;
  margin=0;
  padding=0;
}
<form id="formId">
<table border="1">
  <tr>
    <th>Provide your contact information</th>
    <th>Provide your login access information</th>
  </tr>
  <tr>
    <td>
      <label><span>First Name:</span> <input type = "text" placeholder = "Enter First Name" required/></label>
    </td>
    <td>
      <label><span>Login ID:</span> <input type = "text" placeholder = "type a login ID" required/></label>
    </td>
  </tr>
  <tr>
    <td><label><span>Middle Name:</span> <input type="text" placeholder ="type your middle name"/></label></td>
    <td><label><span>Password:</span> <input type="password" placeholder ="password" required/></label></td>  
  </tr>
  <tr>
    <td><label><span>Last Name:</span> <input type="text" placeholder="Last Name" required/></label></td>
    <td id="but"><label><button type="submit" id="displayButton">Display Info</button></label></td>
  </tr> 
  <tr>
    <td><label><span>Street Address:</span> <input type="text" placeholder="address" required/></label></td>
  </tr>
  <tr>
    <td><label for ="Citylist"><span>City:</span>
      <input type = "text" id ="citylist"
      placeholder="Select a city" list = "cities" required/>
      <datalist id= "cities">
        <option value = "Bronx"/>
        <option value = "Brooklyn"/>
        <option value = "Queens"/>
        <option value = "Manahttan"/>
        <option value = "Staten Island"/>
      </datalist>
      </label>
      </td>
  </tr>
  <tr>
  <td><label for ="StateList"><span>State:</span>
      <input type = "text" id ="State"
      placeholder="Select a city" list = "states" required/>
      <datalist id= "states">
        <option value = "New York"/>
        <option value = "New Jersey"/>
        <option value = "California"/>
        <option value = "Virginia"/>
        <option value = "Maine"/>
      </datalist>
      </td>
  </tr>
  <tr>
    <td><label><span>Zip code:</span> <input type="text" placeholder="Type your zipcode" maxlength="5" required/></label></td>
    </tr>
  <tr>
    <td>
      <label><span>Phone</span>
        <input type ="tel" placeholder="(123)456-789"
        pattern="\(\{3}) +\d{3}-\d{4}" required/>
      </label>
    </td>
  </tr>
  <tr>
    <td>
      <label><span>Email:</span>
        <input type="email" placeholder="[email protected] /cdn-cgi/l/email-protection" required/> 
      </label>
    </td>
  </tr>
  <tr>
    <td>
      <label><span>Birth Date:</span>
        <input type="date" required/>
      </label>
    </td>
  </tr>
</table>
</form>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何为提交按钮添加事件监听器 的相关文章

随机推荐

  • Firebase 扇出 - 最具成本效益的方法?

    我知道这个问题可能已被多次提出 但我已阅读了大多数可用的问题 但没有找到任何可以完全帮助回答我的问题的问题 正如 Firebase 团队提出的 扇出技术是确保快速数据读取的推荐方法 但会带来数据复制的成本 我知道这个问题是主观的 取决于应用
  • 您最喜欢的 SVN Web 应用程序部署工作流程是什么?

    我们目前使用的部署设置有些复杂 涉及远程 SVN 服务器 用于 DEV STAGE 和 PROD 的 3 个 SVN 分支 通过补丁等在它们之间提升代码 我想知道在小型开发团队的情况下您使用什么进行部署 主干用于开发 分支 生产 用于生产
  • 将 MultiIndex 列合并到 pandas 数据框中的单个索引

    在我的代码中 我将 2 个数据库集成到 1 个数据库中 问题是当我向数据库中再添加一列时 结果与预期不符 使用Python 2 7 code import pandas as pd import pandas io formats exce
  • laravel 会话返回 null 尽管设置它

    只是原生 php 中的一个简单函数 protected function some function session start if isset SESSION a SESSION a some value return true els
  • 如何让我的班级充满活力?

    我想要一个带有一个附加属性的字符串 比如说是以红色还是绿色打印它 子类化 str 不起作用 因为它是不可变的 我看到了它的价值 但它可能很烦人 多重继承有帮助吗 我从来没有用过那个 仅继承 object 并使用 self value str
  • 什么可能导致 imagecolorsforindex() 出现“颜色索引超出范围”错误?

    当对一大堆 JPG PNG 和 GIF 文件进行补丁大小调整时 PHP 意外地死机 并显示以下错误消息 imagecolorsforindex function imagecolorsforindex 颜色索引 226 超出范围 相关代码片
  • AspectJ - 更改方法参数的值

    我想要这样的东西 public void doSomething ReplaceFooBar String myString ReplaceFooBar是我的自定义注释 其值应为myString并做一个replaceAll在方法开始执行之前
  • PipEnv:如何处理本地安装的 .whl 包

    我正在使用 PipEnv 设置一个项目 以及一些我需要从预编译的二进制文件安装的包 在以前的项目中 我只是将某些本地文件夹中的 whl 文件安装到我的环境中 但这似乎会导致锁定文件出现问题 如果其他人尝试从存储库安装 因为 pipfile
  • 使用 -NoExit 启动 PowerShell 不起作用

    需要在 PowerShell 中启动多个作业 但它们应该位于不同的会话中 因此 要启动一个 可以使用 Start Process powershell ArgumentList command Get Process noexit nopr
  • 在 woocommerce 结帐页面自定义字段中添加日期

    我正在尝试在 woocommerce 结帐页面中添加自定义选择选项 它正在添加额外的字段 但我想在选择选项的值中添加日期 有什么解决办法吗 这是我在主题 function php 中添加的代码 today new DateTime tomo
  • tfs:如何解锁更改

    我最初编辑了一个文件 该文件进行了结帐 我收到了一台新电脑 现在我想编辑该文件 我不关心原始编辑 TFS 报告另一个用户对该文件具有独占锁定 它实际上不是另一个用户 而是我 但机器不同 因此工作空间不同 我尝试使用以下命令通过 tf 命令行
  • 在海量数据集上学习决策树

    我正在尝试使用 MATLAB 从巨大 即无法存储在内存中 数据集构建二元分类决策树 本质上 我正在做的是 收集所有数据 Try out n数据的决策函数 选出最佳决策函数 https stackoverflow com questions
  • 为不受支持的语言选择本地化

    我有 en 和 ru 语言的本地化 如果用户选择任何其他语言 fr de 我需要显示俄语本地化变体 我尝试将 info plist 中的 本地化本机开发区域 更改为 ru 俄语 但在使用不受支持的语言时 它始终显示英语 有相关问题 http
  • iFrame 内的 cordova 回调

    我使用 cordova 和 nanohttpd 创建了一些 Android Web 应用程序 主页是通过 localhost url 从 nanohttp 加载的 主页包含一个 iFrame 它从与主页相同的域 localhost 加载一些
  • 将 nd 数组转换为键、值字典

    python中是否有一个函数可以将nd数组转换为字典 其中key是索引元组 value是该索引处的矩阵值 例如 A np random random 3 4 5 Result i j k A i j k 当然 你可以使用np ndenume
  • 在 iPhone SDK 中实现 Core-Plot 时出现错误:“CorePlot-CocoaTouch.h:没有这样的文件或目录”

    当我尝试在 iPhone 应用程序中实现 Core Plot 时 出现以下错误 CorePlot CocoaTouch h 没有这样的文件或目录 我从下面的链接下载安装了 Core plot 包 http code google com p
  • 通过电子邮件将 Sparkline 图表作为 Google Sheets 范围内的图像/博客/png 发送

    我尝试将此解决方案应用于我的案例 通过电子邮件发送 SPARKLINE 图表会发送空白单元格而不是数据 https stackoverflow com questions 50133870 emailing sparkline charts
  • S3方法帮助(roxygen2)

    我正在尝试在包中使用 S3 方法 并认为在此处提出问题后我明白了如何设置它 使用 Roxygen 构建 R 包时 S3 方法一致性警告 https stackoverflow com questions 14237018 s3 method
  • 如何在声明式管道中使用 NodeLabel 参数插件

    我正在尝试将我的自由式作业转换为声明性管道作业 因为管道提供了更大的灵活性 我不知道如何使用 NodeLabel 参数插件 https wiki jenkins io display JENKINS NodeLabel Parameter
  • 如何为提交按钮添加事件监听器

    我在这个 html 上遇到了很多麻烦 我正在尝试向提交按钮添加事件侦听器 以便最终可以更改文档以显示表单信息 问题是 当填写表单时 按钮侦听器不执行任何操作 它可以在 jsfiddle 和其他类似的东西中工作 但不能作为独立文件工作 这让我