additionWord.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. } else {
  15. $(".enter.addkeyWord").hide();
  16. $(".showKeyWord").show();
  17. $(".addKeyWord").show();
  18. }
  19. }
  20. hasWords();
  21. // 添加按钮
  22. $(".addKeyWord i").on('click',function(){
  23. window.location.href="/front/wx_dataExport/buyerInput";
  24. });
  25. // 添加keyWords检查输入框内是否有文字,如果有才能点击添加按钮
  26. $('.addkeyWord input.enterOne').on('input', function() {
  27. var buttonDOM = $(this).siblings().find('button')[0]
  28. if ($(this).val().length >= 1) {
  29. buttonDOM.style.opacity = 1
  30. buttonDOM.removeAttribute("disabled")
  31. } else {
  32. buttonDOM.style.opacity = .5
  33. buttonDOM.setAttribute("disabled", true)
  34. }
  35. })
  36. // 添加 按钮的点击事件
  37. $('.addkeyWord .btn .save-btn').on('click', function(){
  38. var keyWord = $('.addkeyWord input.enterOne').val()
  39. keyWord = keyWord.replace(/\s/g,"");
  40. if (keyWord.length > 50) {
  41. // var s = keyWord.slice(0,49);
  42. // $('.addkeyWord input.enterOne').val(s);
  43. weui.toast('采购单位不能超过50字', {
  44. duration: 2000,
  45. className: 'text-overflow100',
  46. callback: function(){}
  47. });
  48. return
  49. }
  50. buyerArr.push(keyWord);
  51. localStorage.setItem("buyer", buyerArr);
  52. var html = `<li>
  53. <div class="one">
  54. <div>
  55. <span>
  56. <p class="key">${ keyWord }</p>
  57. </span>
  58. </div>
  59. <button class="editKeyWord"><i class="iconfont icon-xiugai"></i> 修改</button>
  60. </div>
  61. <div class="modify">
  62. <span contentEditable="true">${ keyWord }</span>
  63. <button class="deleteKey">删除</button>
  64. <button class="ascertainKey">确定</button>
  65. </div>
  66. </li>`
  67. $('.showKeyWord > ul').prepend(html)
  68. // 隐藏
  69. $(".enter.addkeyWord").hide();
  70. $(".showKeyWord").show();
  71. $(".addKeyWord").show();
  72. $('.enter.addkeyWord > input').val('')
  73. var buttonDOM = $('.enter.addkeyWord .btn button')[0]
  74. buttonDOM.style.opacity = .5
  75. buttonDOM.setAttribute("disabled", true)
  76. })
  77. // 编辑
  78. $(".showKeyWord").on('click', '.editKeyWord',function(e){
  79. let oSpan = e.target.parentNode.nextElementSibling.children[0];
  80. let val = $(oSpan).val();
  81. $(this).parent().hide();
  82. $(this).parent().siblings().show().parent().siblings().children('.modify').hide().siblings('.one').show()
  83. $(oSpan).val('').focus().val(val);
  84. $(".addKeyWord").hide();
  85. })
  86. // 编辑 删除
  87. $('.showKeyWord').on('click', '.deleteKey', function(e) {
  88. var buyer = $(this).parent().prev().find('.key').text();
  89. buyerArr.splice($.inArray(buyer, buyerArr),1);
  90. localStorage.buyer = buyerArr.toString();
  91. $(this).parents('li').remove();
  92. $(".addKeyWord").show();
  93. hasWords()
  94. // console.log('删除附加词:',$(this).parent().find('span').text())
  95. })
  96. // 编辑 确定
  97. $('.showKeyWord').on('click', '.ascertainKey', function(e) {
  98. var keyWord = $(this).siblings('textarea').val();
  99. var buyer = $(this).parent().prev().find('.key').text();
  100. keyWord = keyWord.replace(/\s/g,"");
  101. if (keyWord.length < 1) {
  102. weui.toast('采购单位不能为空', {
  103. duration: 2000,
  104. className: 'text-overflow100',
  105. callback: function(){
  106. }
  107. });
  108. return
  109. }else if($(this).parent().find("textarea").val().length > 50){
  110. // var s = $(this).parent().find("span").text().slice(0,49);
  111. // $(this).parent().find("span").text(s);
  112. weui.toast('采购单位不能超过50字', {
  113. duration: 2000,
  114. className: 'text-overflow100',
  115. callback: function(){}
  116. });
  117. return
  118. }else{
  119. for(var i in buyerArr){
  120. if(buyerArr[i] === buyer){
  121. buyerArr[i] = keyWord;
  122. }
  123. }
  124. localStorage.buyer = buyerArr.toString();
  125. $(this).parent().siblings().find('.key').text(keyWord)
  126. $(this).parent().hide().siblings().show()
  127. }
  128. $(".addKeyWord").show();
  129. })
  130. //去空格方法
  131. String.prototype.trim = function(){
  132. return this.replace(/(^\s*)|(\s*$)/g, ' ');
  133. }
  134. $(".close").on("click", function () {
  135. history.back();
  136. })
  137. });