jQuery在模糊上无法正常工作 [英] jQuery on blur not working properly

查看:98
本文介绍了jQuery在模糊上无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的教育详细信息页面,我在其中动态生成文本框以输入资格详细信息.当我尝试验证文本框时,将触发验证,但验证不正确.如果我尝试2-3次,它将变得正确.在第一次尝试中,备用文本框将得到验证.当我单击添加行"按钮时,将添加新行,但是当我尝试对其进行2-3次验证时,不会显示验证错误.再进行几次尝试后,验证便会生效.这一切都很混乱.

I have a simple educational details page where I am generating textboxes dynamically to enter qualification details. When I try to validate the textboxes, the validation gets fired but not properly. If I try it 2-3 times then it becomes proper. In the first attempt alternate textboxes get validated. When I click on "add row" button a new row gets added but when I try to validate it for 2-3 attempts the validation error is not shown. After few more attempts the validation works. It is all very confusing.

我在互联网上进行了很多研究,但找不到任何合适的解决方案.

I researched a lot on the internet but could not find any proper solution.

我在下面粘贴了完整的JSP.真的非常感谢任何指针.

I have pasted my full JSP below. Will be really really thankful for any pointers.

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Enter Details</title>

        <link rel="stylesheet" type="text/css" href="../css/jquery.validate.css" />
        <link rel="stylesheet" type="text/css" href="../css/style.css" />

        <script src="../js/jquery-1.3.2.js" type="text/javascript"></script>
        <script src="../js/jquery-1.9.1.js" type="text/javascript"></script>
        <script src="../js/jquery.validate.js" type="text/javascript"></script>
        <script src="../js/jquery.validation.functions.js" type="text/javascript"></script>
       <script type="text/javascript">       
       $(document).ready(function(){
       jQuery(".validate-text").on("blur", function (event) {
           setTimeout(function() {
               jQuery(".validate-text").validate({
            expression: "if (VAL) return true; else return false;",
            message: "Please enter value."
            }); });
    });
       jQuery(".validate-num").on("blur", function (event) {
           setTimeout(function() {
               jQuery(".validate-num").validate({
            expression: "if (!isNaN(VAL) && VAL) return true; else return false;",
            message: "Please enter value."
            });
           });
    });
       });
        </script>
        <script type="text/javascript">

        $(document).ready(function(){

         $("#addRow").click(function() {
            var i=0;
            i= $("#countHd").val();
            i= parseInt($("#countHd").val(),10); 
             $("table").append('<tr>'+
                    '<td class="MasterTabletd"><input type="text" class="validate-text" name="courseTxt'+i+'" id="courseTxt'+i+'" /></td>'+
                    '<td class="MasterTabletd"><input type="text" class="validate-text" name="boardTxt'+i+'" id="boardTxt'+i+'" /></td>'+
                    '<td class="MasterTabletd"><input type="text" class="validate-num"  style="width: 80px;" name="marksTxt'+i+'" id="marksTxt'+i+'" /></td>'+
                    '<td class="MasterTabletd"><input type="text" class="validate-num"  style="width: 80px;" name="totalTxt'+i+'" id="totalTxt'+i+'"  /></td>'+
                    '<td class="MasterTabletd"><input type="text" style="width: 80px;" name="percentageTxt'+i+'" id="percentageTxt'+i+'" style="width: 80px;"  /></td>'+
                    '<td class="MasterTabletd"><input type="text" class="validate-text"  style="width: 80px;" name="classTxt'+i+'" id="classTxt'+i+'"  /></td>'+
                '</tr>');
             i=i+1;
             $("#countHd").val(i); 
            });
        });


        </script>
    </head>
    <body>
    <div id="contain">

<div id="header">
<h3>Enter Details</h3>
</div>

<form id="personalDetails" class="MasterForm" method="post"
    action="AddPersonalDetails.php">
<table class="MasterTable">
    <tr><td class="MasterTabletd">
    <input type="button" name="addRow" id="addRow" class="submitbutton" value="Add Row" /></td>
    <td class="MasterTabletd">
    <input type="submit" name="educational" id="educational" class="submitbutton" value="Submit" /></td>
    </tr>

    <tr>
    <td>Course Name<span class="required">*</span></td>
    <td>Board / University<span class="required">*</span></td>
    <td>Marks Obtained<span class="required">*</span></td>
    <td>Total Marks<span class="required">*</span></td>
    <td>Percentage<span class="required">*</span></td>
    <td>Year of Completion<span class="required">*</span></td>
    </tr>
    <tr>
        <td class="MasterTabletd"><input type="text" name="courseTxt" id="courseTxt" class="validate-text" /></td>
        <td class="MasterTabletd"><input type="text" name="boardTxt" id="boardTxt" class="validate-text" /></td>
        <td class="MasterTabletd"><input type="text" name="marksTxt" id="marksTxt" class="validate-num" /></td>
        <td class="MasterTabletd"><input type="text" name="totalTxt" id="totalTxt" class="validate-num" /></td>
        <td class="MasterTabletd"><input type="text" name="percentageTxt" id="percentageTxt" /></td>
        <td class="MasterTabletd"><input type="text" name="classTxt" id="classTxt" class="validate-text"  /></td>
    </tr>
</table>
<input type="hidden" name="countHd" id="countHd" value="1" />
</form>
</div>      
    </body>
</html>

谢谢.

推荐答案

由于您正在动态创建这些元素,因此请尝试授权您的活动:

Since you're creating those elements dynamically, try delegating your events:

jQuery(document).on("blur",".validate-text", function (event) { 
// repeat for ".validate-num"

此外,您不应该使用两种不同版本的jQuery.删除<script src="../js/jquery-1.3.2.js" type="text/javascript"></script>

Additionally, you shouldn't be using two different versions of jQuery. Remove <script src="../js/jquery-1.3.2.js" type="text/javascript"></script>

这篇关于jQuery在模糊上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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