如何使用 javascript 在一个表单中验证不同的输入提交按钮

How to validate different input submit buttons in one form using javascript

本文关键字:验证 按钮 提交 输入 表单 一个 javascript 何使用      更新时间:2023-09-26

我试图使下面的代码只显示"你确定要继续删除选定的吗?"javascript消息,只有当我点击我的表单的删除输入按钮时。有人可以帮我吗?其他输入按钮我只想显示"您没有任何选定的文件"。

我的复选框[]是一个数组,这些值是通过mysql/php生成的。

  <script type = "text/javascript">
  function confirm_update() {
  var chkCount = 0;
  var arrCheckboxes = document.formtop.elements["checkbox[]"];
  for (var i=0; i<arrCheckboxes.length; i++) {
    if(arrCheckboxes[i].checked == true) {
        chkCount++;
    }
  }
    if (chkCount === 0) {
    alert("You do not have any selected files.");
    return false;
   } else {
      return confirm("Are you sure you want to proceed deleting the selected?");
  }
 }
 </script>



  <form   name="formtop"  onsubmit="return confirm_update();"  method="POST" action=""    >
<table border="0" cellspacing="0" cellpadding="4" bgcolor="#FFFFFF" id="Text_Buttons" >
 <tr>
<td align="center"><input id="content-button" style="width:90px;" type="Button"    name="Preferences_button" value="Edit Ad" ></td>     
<td align="center"><input id="content-button" style="width:90px;" type="submit" name="delete" value="Delete Ad" ></td>
 <td align="center"><input id="content-button" style="width:90px;" type="submit" name="pause" value="Pause" ></td>
<td align="center"><input id="content-button" style="width:90px;" type="submit" name="resume" value="Resume" ></td>
 </tr>
 </table>

 <input type="checkbox" name="checkbox[]" value="1" />
</form>

将按钮从type="submit"更改为type="button",然后添加一个事件侦听器,用于"单击"它们,您可以在其中显示消息,然后提交表单。