Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 键控时返回false时出错_Jquery_Html - Fatal编程技术网

Jquery 键控时返回false时出错

Jquery 键控时返回false时出错,jquery,html,Jquery,Html,这里是html <form name="passwordchng" method="post" action=""> <input name="_curpasswd" id="curpasswd" size="20" autocomplete="off" type="password" /> <input name="_newpasswd" id="newpasswd" size="20" autocomplete="off" t

这里是html

    <form name="passwordchng" method="post" action="">
       <input name="_curpasswd" id="curpasswd" size="20" autocomplete="off" type="password" />
       <input name="_newpasswd" id="newpasswd" size="20" autocomplete="off" type="password" />
       <input name="_confpasswd" id="confpasswd" size="20" autocomplete="off" type="password" />
       <input type="submit" name="submit" value="Save" />
    </form>

这里是jquery

  <script> 
    $("#newpasswd").keyup(function() {
        if($("#curpasswd").val()==$("#newpasswd").val()){
            $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("No changes made to your password.");
            return false;
        }
        else{
          $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("A strong Password should have at least 1 alphabet,1 digit and 1 special character."); 
          return true;
        }
    });
 </script>

$(“#newpasswd”).keyup(函数(){
if($(“#curpasswd”).val()==$(“#newpasswd”).val()){
$(“.newpasseror”).css({“color”:“red”,“left”:“10px”}).text(“密码未更改”);
返回false;
}
否则{
$(“.newpasseror”).css({“颜色”:“红色”,“左边距”:“10px”}).text(“强密码应至少包含1个字母、1个数字和1个特殊字符”);
返回true;
}
});

当条件执行时,它不应允许提交表单。此处表单正在提交,即使两个密码相同…

您可以使用以下命令在if条件下禁用提交按钮:

 $('input[type="submit"]').attr('disabled','disabled');
 $('input[type="submit"]').removeAttr('disabled');
并使用以下命令在else条件下再次启用:

 $('input[type="submit"]').attr('disabled','disabled');
 $('input[type="submit"]').removeAttr('disabled');
完整代码:

 $("#newpasswd").keyup(function() {
  if($("#curpasswd").val()==$("#newpasswd").val())
    {
    $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("No changes made to your password.");
    $('input[type="submit"]').attr('disabled','disabled');
}
else
{
    $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("A strong Password should have at least 1 alphabet,1 digit and 1 special character."); 
    $('input[type="submit"]').removeAttr('disabled');
}});

如果要返回true或false,请在
submit
上使用验证,而不是
keyup
。比如:

$("#passwordchng").submit(function() {

    if($("#curpasswd").val()==$("#newpasswd").val())
    {
        $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("No changes made to your password.");
        return false;
    }

    else
    {
        $(".newpasserror").css({"color":"red","margin-left":"10px"}).text("A strong Password should have at least 1 alphabet,1 digit and 1 special character.");
        return true;
    }

 });
注意:Id是给表单的


试试这个..

<form name="passwordchng" method="post" action="">
  <input name="_curpasswd" id="curpasswd" size="20" autocomplete="off" type="password" />
   <input name="_newpasswd" id="newpasswd" size="20" autocomplete="off" type="password" onChange="password();"/>
    <input name="_confpasswd" id="confpasswd" size="20" autocomplete="off" type="password" />
  <input type="submit" name="submit" value="Save" />
    <div id="message"></div>
   </form>
<script>
function password() {

    var curpasswd = $("#curpasswd").val();
    var newpasswd = $("#newpasswd").val();

    if (curpasswd != newpasswd)
        $("#message").html("Passwords do not match!");
    else
        $("#message").html("Passwords match.");
}

$(document).ready(function () {
   $("#newpasswd").keyup(password);
});

</script>

函数密码(){
var curpasswd=$(“#curpasswd”).val();
var newpasswd=$(“#newpasswd”).val();
if(curpasswd!=newpasswd)
$(“#message”).html(“密码不匹配!”);
其他的
$(“#message”).html(“密码匹配”);
}
$(文档).ready(函数(){
$(“#newpasswd”).keyup(密码);
});

尝试使用
keydown
代替
keydup
如果您想阻止表单提交,您应该收听提交事件而不是keydup。