javascript
  • jquery
  • validation
  • jquery-plugins
  • 2015-11-06 109 views 0 likes 
    0

    我試圖動態地將驗證規則添加到一些使用http://jqueryvalidation.org/的動態控件,但它不起作用。如何使用jQuery驗證插件動態添加驗證規則

    $(".rtxt").each(function() { 
          $(this).rules('add', { 
           required: true 
          }); 
         }); 
    
    <input class='rtxt' name='txtDocName' id='txtDocName' style='width:220px;' type='text' > 
    

    不知道我在這裏失蹤。

    我不能使用'required'屬性作爲它不支持的IE9,所以我將不得不使用jquery驗證插件。

    這裏是小提琴

    http://jsfiddle.net/ed4fg1xo/

    而且,我需要做的div點擊事件驗證。

    回答

    0
    $(document).ready(function() { 
        // 1. prepare the validation rules and messages. 
        var rules = { 
         textbox1: { 
          required: true, 
          minlength: 2 
         }, 
         textbox2: "required", 
         textbox3: "required" 
        }; 
        var messages = { 
         textbox1: { 
          required: "textbox1 is required", 
          minlength: "textbox1 needs to be at least length 2" 
         }, 
         textbox2: "textbox2 is requried", 
         textbox3: "textbox3 is required" 
        }; 
    
        // 2. Initiate the validator 
        var validator 
         = new jQueryValidatorWrapper("FormToValidate", 
          rules, messages); 
    
        // 3. Set the click event to do the validation 
        $("#DivIdName").click(function() { 
         if (!validator.validate()) 
          return; 
    
         alert("Validation Success!"); 
        }); 
    }); 
    
    +0

    我需要放置$(「#yourformname」)。validate(); 我需要驗證div點擊事件控件。 – user1263981

    +0

    查看此鏈接:http://forums.asp.net/t/1884112.aspx?使用+ jquery + validation + plugin + for + div + instead + of表單 – sanjay

    +0

    我需要放置$(「#yourformname」 ).validate();在div點擊事件? – user1263981

    相關問題