返回 false 不起作用 [英] return false is not working

查看:30
本文介绍了返回 false 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用动态创建的复选框创建了一个表单.我使用了一个 j 查询脚本来检查天气用户是否选中了至少一个复选框.如果不是,那么它会提醒错误消息.

I have created one form with dynamically created check box's . And i have used one j query script that will check weather user has checked at-least one check box or not .Tf it is not then it will alert the error message .

HTML 代码

print "<form action=\"dpt_multi_delete.php\" method=\"POST\" name=\"searchform\"  >\n";

print "<td><input type=\"checkbox\" name=\"dptid$i\" value=\"$row[dpt_id]\"></td>\n";

print "<input type=\"submit\" name=\"submit\" value=\"DELETE SELECTED\" onclick=\"Validate()\"/>\n"; 

java 脚本是:

function Validate() {
 var form = document.forms[0]
  var counter=0;
  for (i = 0; i < form.elements.length ; i++) {
    if (form.elements[i].type == "checkbox"){
      if(form.elements[i].checked == true){
   counter++
     }
    }
    }
    if (counter == 0){
     alert("You have not selected an item to delete");
      return false;
      }
   else{
     alert("Are you sure you want to delete  "+counter+" records");

     }
    }

问题 1:我面临的问题是,如果用户没有选中任何复选框,那么他将收到错误警报.单击确定或取消后,它将重定向到 dpt_multi_delete.php 页面.即使我在警报后使用了返回假"但它不起作用.

Problem 1: the problem what i am facing is if user is not selected any checkboxs then he will get error alert . after clicking that ok or cancel it is redirecting to dpt_multi_delete.php page. even i used "return false" after alert but its not working.

问题 2:如果用户选择了多个复选框,那么我会发出警告,然后如果用户点击确定,那么它将删除记录.但是在取消该警报消息后就删除了记录.

Problem 2: if the user selected multiple checkboxes then i will alert the warning then if the user clicks ok then it will delete the record . but is all so deleting record after canceling that alert message .

这是什么称呼.?我错在哪里?

what is the salutation for this . ?where i am wrong ?

推荐答案

如果您打算从事件处理函数中返回 false,那么您需要从 事件处理函数 本身中进行,并且不仅仅是它调用的另一个函数.

If you are going to return false from an event handler function, then you need to do it from the event hander itself, and not just another function that it calls.

您的 onclick 函数没有 return 语句.它调用 Validate,然后对函数返回的值不做任何处理.

Your onclick function doesn't have a return statement. It calls Validate and then does nothing with the value that function returns.

onclick="return Validate();"

<小时>

约定为构造函数保留了以大写字母开头的变量名.调用您的函数 validate,而不是 Validate.

在提交表单而不是单击特定提交按钮时处理此类测试通常是一个更好的主意.

It is generally a better idea to handle this kind of test when the form is submitted rather than when a particular submit button is clicked.

现代 JavaScript 不使用 onclick/onsubmit/etc 属性.我们使用 addEventListener 代替.

Modern JavaScript doesn't use onclick/onsubmit/etc attributes. We use addEventListener instead.

function validate(event) {
    // Your logic
    if (someCondition) {
        // Stop
        event.preventDefault();
    }
}

document.querySelector("form").addEventListener('submit', validate);

这篇关于返回 false 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆