jQuery Validation PlugIn : Callback for field success validation?

你说的曾经没有我的故事 提交于 2019-12-19 10:27:59

问题


Im am using errorPlacement to add a class to a label to show a graphic element in addition to the error text ( they are both different elements).

Is there a way to use a callback that will fire when the validation of a field is successful? I am already using validClass, that will change the error message, but I need a call back so I can change the graphic element to a "success" one.

For Example

[graphic] [input field] [error text]

I was able to change the [graphic] and the [error text] when there is an error, but when the correct value is entered in the field I was able to change the [error text] but not the [graphic].


回答1:


You might want to check out the success option.

http://docs.jquery.com/Plugins/Validation/validate#options

$("#myform").validate({
   success: function(label) {
     label.addClass("valid").text("Ok!")
   },
   submitHandler: function() { alert("Submitted!") }
})

If you pass a function to success, you are given back the label as a jQuery object. You can then manipulate that object or a related object.


Alternatively, your error and success graphic can use a background image, and then you can swap out the background image with CSS on the validClass.




回答2:


You can override the defaults validClass: "valid" with validClass:"success"



来源:https://stackoverflow.com/questions/3518895/jquery-validation-plugin-callback-for-field-success-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!