首页 新闻 赞助 找找看

为什么这个 的 n3=0 注销了 会出错?each函数里的 n3 不是赋值吗,为什么前面还要先赋值才不出错

0
[待解决问题]
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="js/jquery-3.6.0.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/jquery-migrate-1.4.1.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/jquery-migrate-3.3.2.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			span{
				color: red;
				font-weight: bold;
				display: none;
			}
		</style>
	</head>
	<body>
		
		<!--
			***********  表单提交判断      ************
		-->
		<form action="sadfad.php" method="post">
			<input type="text" name="username" id="username" value="" placeholder="user" class="op"/> 
			<span id="">错了</span>
			
			<input type="password" name="passsword" id="passsword" value="" placeholder="密码" class="op" />
			<span id="">错了</span>
				
			<input type="submit" value="提交"/>
		</form>
		<script type="text/javascript">


          $('input[type=text]').blur(function(){
          	if(this.value.length<6){
          		$(this).next().show();
          		$(this).data('num',0);
          		
          	}else{
          		$(this).next().hide();
          		$(this).data('num',1);
          	}
          });
			 $('input[type=password]').blur(function(){
			          	if(this.value.length<10){
			          		$(this).next().show();
			          		$(this).data('num',0);
			          	}else{
			          		$(this).next().hide();
			          		$(this).data('num',1);
			          		
			          	}
			          });
			          
			          $('form').submit(function(){
			          	$('.op').blur(); //先触发前面几个的验证
			          	
			          	
			          //****** n3 = 0  **********	
                     //这里的 n3 不先赋值0,为什么会报错
//                    n3 = 0;
                       $('.op').each(function(){
                       	n3+=$(this).data('num');
                       });
                        alert(n3);
                        
			          	if(n3==2){
			          		alert('验证通过,提交,')
			          		//实际情况时取消这里的return false
			          		
			          		return false;
			          	}else{
			          		
			          		alert('错误,不提交')
			          		return false;
			          	}
//                        return false;
			          });
		</script>		
	</body>
</html>

问题补充:
	n3+=$(this).data('num');

这里的n3,前面不赋值为什么错

牧_童的主页 牧_童 | 菜鸟二级 | 园豆:210
提问于:2021-07-30 20:47
< >
分享
所有回答(1)
0

n3未定义
n3+=$(this).data('num');相当于n3=n3+$(this).data('num');会报undefined
n3=0相当于初始化,前面不加var会提升为全局变量

Dupont | 园豆:157 (初学一级) | 2021-08-09 16:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册