tougao.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. +(function($){
  2. /* tougao
  3. * ====================================================
  4. */
  5. var tougao = {
  6. init: function(){
  7. var that = this
  8. that._title = $('#tougao-title')
  9. that._url = $('#tougao-url')
  10. that._content = $('#tougao-content')
  11. that._submit = $('#tougao-submit')
  12. that._check = [, 0, 0]
  13. $('#tougao').on('shown', function(){
  14. that._title.focus()
  15. })
  16. that._title.blur(function(){
  17. that.check_title( $(this) )
  18. })
  19. that._url.blur(function(){
  20. that.check_url( $(this) )
  21. })
  22. that._content.blur(function(){
  23. that.check_content( $(this) )
  24. })
  25. that._submit.bind('click', function(){
  26. if( !that.is_check() ){
  27. that.check_title( that._title )
  28. that.check_url( that._url )
  29. that.check_content( that._content )
  30. return
  31. }
  32. var _tip = that._submit.prev()
  33. $.ajax({
  34. type: 'POST',
  35. url: _deel.url+'/ajax/tougao.php',
  36. data: 'title='+$.trim(that._title.val())+'&url='+$.trim(that._url.val())+'&content='+$.trim(that._content.val()),
  37. success: function(data){
  38. if(data === 'sofast'){
  39. _tip.show().html('服务器忙,请稍候重试!')
  40. setTimeout(function(){
  41. _tip.fadeOut(1000)
  42. }, 5000)
  43. }
  44. if(data === 'success'){
  45. _tip.show().addClass('text-success').html('投稿成功,审核通过后将正式发布!')
  46. setTimeout(function(){
  47. $('#tougao').modal('hide')
  48. that._title.val('')
  49. that._url.val('')
  50. that._content.val('')
  51. _tip.hide()
  52. that._title.focus()
  53. }, 4000)
  54. }
  55. if(data === 'fail'){
  56. _tip.show().html('投稿失败,请稍候重试!')
  57. setTimeout(function(){
  58. _tip.fadeOut(1000)
  59. }, 5000)
  60. }
  61. if(data === 'title'){
  62. _tip.show().html('标题不能为空,且不能大于40个字符!')
  63. }
  64. if(data === 'url'){
  65. _tip.show().html('网址不能为空,且不能大于100个字符!')
  66. }
  67. if(data === 'content'){
  68. _tip.show().html('内容不能为空,且介于200-5000个字符之间!')
  69. }
  70. }
  71. });
  72. })
  73. },
  74. is_check: function(){
  75. return this._check.join('') === '111' ? true : false
  76. },
  77. check_title: function(target){
  78. if(!target.val() || target.val().length < 8){
  79. this.tip(target,'标题太短,不得少于8字!', 0);
  80. }
  81. else if(target.val().length > 30){
  82. this.tip(target,'标题太长,不得超过30字!', 0);
  83. }
  84. else{
  85. this.tip_hide(target, 0)
  86. }
  87. },
  88. check_url: function(target){
  89. if(!target.val() || !target.val().match(/^(http|https):\/\/([a-z0-9-]{1,}.)?[a-z0-9-]{2,}.([a-z0-9-]{1,}.)?[a-z0-9]{2,}/)){
  90. this.tip(target,'格式错误!', 1)
  91. }
  92. else{
  93. this.tip_hide(target, 1)
  94. }
  95. },
  96. check_content: function(target){
  97. if(!target.val() || target.val().length < 200){
  98. this.tip(target,'内容太短,不得少于200字', 2)
  99. }
  100. else if(target.val().length > 5000){
  101. this.tip(target,'内容太长,不得超过5000字', 2)
  102. }
  103. else{
  104. this.tip_hide(target, 2)
  105. }
  106. },
  107. tip: function(id, txt, c){
  108. id.next('.text-error').html(txt).slideDown(300)
  109. this._check[c] = 0
  110. },
  111. tip_hide: function(id, c){
  112. id.next('.text-error').slideUp(300)
  113. this._check[c] = 1
  114. }
  115. }
  116. tougao.init()
  117. })(window.jQuery);