additionWord.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. $(function(){
  2. $('.knowBtn').on('click',function(){
  3. $(".problemPop").hide()
  4. })
  5. $(".problem").on('click',function(){
  6. $(".problemPop").css("display",'flex');
  7. })
  8. function hasWords () {
  9. var showKeyWordLength = $(".showKeyWord ul").find('li').length;
  10. if(showKeyWordLength === 0){
  11. $(".enter.addkeyWord").show();
  12. $(".addKeyWord").hide();
  13. $(".showKeyWord").hide();
  14. $('#add-buyer-word').hide()
  15. } else {
  16. $(".enter.addkeyWord").hide();
  17. $(".showKeyWord").show();
  18. $(".addKeyWord").show();
  19. if (showKeyWordLength >= buyerLimit) {
  20. $('#add-buyer-word').hide()
  21. $('.buyer-more-tips').show()
  22. } else {
  23. $('#add-buyer-word').show()
  24. $('.buyer-more-tips').hide()
  25. }
  26. }
  27. }
  28. hasWords();
  29. // 添加按钮
  30. $(".addKeyBtn i").on('click',function(){
  31. window.location.href="/front/wx_dataExport/buyerInput";
  32. });
  33. $('textarea').each(function(i,dom){
  34. // console.log(i,dom)
  35. dom.style.height = dom.scrollHeight +'px';
  36. });
  37. $("textarea").on("input", function() {
  38. this.style.height = 'auto';
  39. this.style.height = this.scrollHeight + "px";
  40. });
  41. var u = navigator.userAgent;
  42. var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  43. var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  44. if(isAndroid){
  45. //防止键盘把当前输入框给挡住
  46. $('input[type="text"],textarea').focus(function () {
  47. var target = this;
  48. setTimeout(function(){
  49. target.scrollIntoViewIfNeeded();
  50. },400);
  51. });
  52. }
  53. // 添加keyWords检查输入框内是否有文字,如果有才能点击添加按钮
  54. $('.addkeyWord input.enterOne').on('input', function() {
  55. var buttonDOM = $(this).siblings().find('button')[0]
  56. if ($(this).val().replace(/\s+/g,"").length >= 1) {
  57. buttonDOM.style.opacity = 1
  58. buttonDOM.removeAttribute("disabled")
  59. } else {
  60. buttonDOM.style.opacity = .5
  61. buttonDOM.setAttribute("disabled", true)
  62. }
  63. })
  64. // 添加 按钮的点击事件
  65. $('.addkeyWord .btn .save-btn').on('click', function(){
  66. var keyWord = $('.addkeyWord input.enterOne').val()
  67. keyWord = keyWord.replace(/(^\s*)|(\s*$)/g, "");
  68. if (keyWord.length > 50) {
  69. // var s = keyWord.slice(0,49);
  70. // $('.addkeyWord input.enterOne').val(s);
  71. weui.toast('采购单位不能超过50字', {
  72. duration: 2000,
  73. className: 'text-overflow100',
  74. callback: function(){}
  75. });
  76. return
  77. }
  78. buyerArr.push(keyWord);
  79. localStorage.setItem("buyer", buyerArr);
  80. var html = `<li>
  81. <div class="one">
  82. <div>
  83. <span>
  84. <p class="key">${ keyWord }</p>
  85. </span>
  86. </div>
  87. <button class="editKeyWord"><i class="iconfont icon-xiugai"></i> 修改</button>
  88. </div>
  89. <div class="modify">
  90. <textarea name="" rows="1" placeholder="" maxlength="">${ keyWord }</textarea>
  91. <button class="deleteKey">删除</button>
  92. <button class="ascertainKey">确定</button>
  93. </div>
  94. </li>`
  95. $('.showKeyWord > ul').prepend(html)
  96. // 隐藏
  97. $(".enter.addkeyWord").hide();
  98. $(".showKeyWord").show();
  99. $(".addKeyWord").show();
  100. $('.enter.addkeyWord > input').val('')
  101. var buttonDOM = $('.enter.addkeyWord .btn button')[0]
  102. buttonDOM.style.opacity = .5
  103. buttonDOM.setAttribute("disabled", true)
  104. hasWords()
  105. })
  106. // 编辑
  107. $(".showKeyWord").on('click', '.editKeyWord',function(e){
  108. let oSpan = $(this).parent().siblings().children('textarea');
  109. let val = $(oSpan).val();
  110. $(this).parent().hide();
  111. $(this).parent().siblings().show().parent().siblings().children('.modify').hide().siblings('.one').show()
  112. $(oSpan).val('').focus().val(val);
  113. $(".addKeyWord").hide();
  114. hasWords()
  115. })
  116. // 编辑 删除
  117. $('.showKeyWord').on('click', '.deleteKey', function(e) {
  118. var buyer = $(this).parent().prev().find('.key').text();
  119. buyerArr.splice($.inArray(buyer, buyerArr),1);
  120. localStorage.buyer = buyerArr.toString();
  121. $(this).parents('li').remove();
  122. $(".addKeyWord").show();
  123. hasWords()
  124. // console.log('删除附加词:',$(this).parent().find('span').text())
  125. })
  126. // 编辑 确定
  127. $('.showKeyWord').on('click', '.ascertainKey', function(e) {
  128. var keyWord = $(this).siblings('textarea').val();
  129. var buyer = $(this).parent().prev().find('.key').text();
  130. keyWord = keyWord.replace(/(^\s*)|(\s*$)/g, "");
  131. if (keyWord.replace(/\s+/g,"").length < 1) {
  132. weui.toast('采购单位不能为空', {
  133. duration: 2000,
  134. className: 'text-overflow100',
  135. callback: function(){
  136. }
  137. });
  138. return
  139. }else if($(this).parent().find("textarea").val().length > 50){
  140. // var s = $(this).parent().find("span").text().slice(0,49);
  141. // $(this).parent().find("span").text(s);
  142. weui.toast('采购单位不能超过50字', {
  143. duration: 2000,
  144. className: 'text-overflow100',
  145. callback: function(){}
  146. });
  147. return
  148. }else{
  149. for(var i in buyerArr){
  150. if(keyWord !== buyer){
  151. if(buyerArr[i] === keyWord){
  152. weui.toast('采购单位重复了', {
  153. duration: 2000,
  154. className: 'text-overflow100',
  155. callback: function(){}
  156. });
  157. return
  158. }
  159. }
  160. if(buyerArr[i] === buyer){
  161. buyerArr[i] = keyWord;
  162. }
  163. }
  164. localStorage.buyer = buyerArr.toString();
  165. $(this).parent().siblings().find('.key').text(keyWord)
  166. $(this).parent().hide().siblings().show()
  167. }
  168. // $(".addKeyWord").show();
  169. hasWords()
  170. })
  171. //去空格方法
  172. String.prototype.trim = function(){
  173. return this.replace(/(^\s*)|(\s*$)/g, ' ');
  174. }
  175. $(".close").on("click", function () {
  176. history.back();
  177. })
  178. });