This commit is contained in:
绯村月
2014-03-11 14:55:50 +08:00
parent 106c7f7d78
commit 8a7a41229b
3 changed files with 49 additions and 46 deletions

View File

@@ -0,0 +1,44 @@
$ ->
$.fn.checkAndRequest = (url, callback, opts) ->
defaults = {}
opts = $.extend defaults, opts
form = $(@)
error = false
data = {}
$('#page-alert').show().find('button').nextAll().remove()
for k, v of opts
item = form.find "##{k}"
formGroup = item.closest '.form-group'
method = v['check']
result = switch typeof method
when "object" then method.test item.val()
when "function" then method()
when "string"
rt = switch method
when 'required'
item.val() isnt ''
when ''
true
else
false
else
false
if result
formGroup.addClass 'has-success'
data[k] = item.val()
else
formGroup.addClass 'has-error'
$('#page-alert').append "<p>#{v['error']}</p>"
error = true
if not error
$('#page-alert').hide()
$.ajax
url: url
method: 'post'
data: data
success: callback