JS页面显示事件触发表单验证(JS pageshow event triggerring on form validation)

编程入门 行业动态 更新时间:2024-10-18 01:28:12
JS页面显示事件触发表单验证(JS pageshow event triggerring on form validation)

所以我有以下JS来显示加载GIF,而我的servlet添加一些数据,然后重定向。 当我提交表单时,它会触发点击事件没问题。 但是在它运行页面显示之后立即再次隐藏div。 这对我来说毫无意义,我仍然在同一页上。 页面显示的重点是,如果有人点击回来,页面不会卡在加载GIF上。 为什么在验证检查后会触发页面显示? 我错过了什么

// ==== [START] Search gif hide and show ====
$("#equipSrch").click(function () {
    if ($("#search-form").valid()) {
        document.getElementById("viewSrch").style.display = 'none';
        document.getElementById("loading").style.display = 'block';
    }
});

$(window).bind('pageshow', function () {
    document.getElementById("viewSrch").style.display = 'block';
    document.getElementById("loading").style.display = 'none';
});
// ==== [END] Search gif hide and show ==== 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
  
 

So I have the below JS to show a loading GIF while my servlet adds some data and then redirects. When I submit the form it fires the on click event no problem. But then right after it runs the pageshow and hides the div again. Which makes no sense to me, I'm still on the same page. The point of the pageshow was if someone clicked back, the page doesn't get stuck on the loading GIF. Why is the pageshow being triggered after the validation check? What am I missing

// ==== [START] Search gif hide and show ====
$("#equipSrch").click(function () {
    if ($("#search-form").valid()) {
        document.getElementById("viewSrch").style.display = 'none';
        document.getElementById("loading").style.display = 'block';
    }
});

$(window).bind('pageshow', function () {
    document.getElementById("viewSrch").style.display = 'block';
    document.getElementById("loading").style.display = 'none';
});
// ==== [END] Search gif hide and show ==== 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
  
 

最满意答案

我错过了一些jquery导入,有效的()函数正在触发但是我之前错过了一个异常。 我也没能先调用validate()函数。

	// ==== [START] Search gif hide and show ====
	$("#submit").click(function() {
		$("#search-form").validate();
		if($("#search-form").valid()) {
			document.getElementById("viewSrch").style.display = 'none';
			document.getElementById("loading").style.display = 'block';
		}
	});
	
	$(window).bind('pageshow', function() {
		document.getElementById("viewSrch").style.display = 'block';
		document.getElementById("loading").style.display = 'none';
	 });
	// ==== [END] Search gif hide and show ==== 
  
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script> 
  
 

I was missing some jquery imports, the valid() function was firing but getting an exception I missed before. Also I failed to call the validate() function first.

	// ==== [START] Search gif hide and show ====
	$("#submit").click(function() {
		$("#search-form").validate();
		if($("#search-form").valid()) {
			document.getElementById("viewSrch").style.display = 'none';
			document.getElementById("loading").style.display = 'block';
		}
	});
	
	$(window).bind('pageshow', function() {
		document.getElementById("viewSrch").style.display = 'block';
		document.getElementById("loading").style.display = 'none';
	 });
	// ==== [END] Search gif hide and show ==== 
  
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script> 
  
 

更多推荐

本文发布于:2023-08-06 18:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   事件   页面   JS   pageshow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!