v1.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * 处理输入框值变化。
  3. * Handling input box value changes.
  4. *
  5. * @param Event e Event object
  6. * @returns void
  7. */
  8. window.changeInput = function(e)
  9. {
  10. const value = $(this).val();
  11. const intValue = value < 1 ? 1 : parseInt(value, 10);
  12. $(this).val(intValue);
  13. }
  14. /**
  15. * 处理输入框值变化。
  16. * Handling input box value changes.
  17. *
  18. * @param Event e Event object
  19. * @returns void
  20. */
  21. window.changePercentInput = function(e)
  22. {
  23. const value = $(e.target).val();
  24. const intValue = value ? parseInt(value, 10) : '';
  25. $(e.target).val(intValue);
  26. }
  27. /**
  28. * 输入框值限制只能输入小数点后两位的数字。
  29. * The input box value is limited to only two decimal places.
  30. *
  31. * @param Event e Event object
  32. * @returns void
  33. */
  34. window.changePriceInput = function(e)
  35. {
  36. const value = $(e.target).val();
  37. const regex = /^\d*\.?\d{0,2}$/;
  38. if (!regex.test(value)) $(this).val(value.slice(0, -1));
  39. }
  40. /**
  41. * 设置行号。
  42. * Set line number.
  43. *
  44. * @param number canAddRows
  45. * @param number fieldsCount
  46. * @access public
  47. * @return void
  48. */
  49. function setLineIndex(canAddRows, fieldsCount)
  50. {
  51. let index = 0;
  52. $('.rows-group').each(function()
  53. {
  54. const resultIndex = fieldsCount + index;
  55. $(this).find('[id^="customFields"]').attr('name', 'customFields[' + resultIndex + ']');
  56. $(this).find('[id^="result"]').attr('name', 'result[' + resultIndex + ']');
  57. $('.add-rows').toggleClass('disabled', index + 1 >= canAddRows);
  58. index ++;
  59. });
  60. }
  61. /**
  62. * 添加行。
  63. * Add rows.
  64. *
  65. * @access public
  66. * @returns void
  67. */
  68. $(document).off('click', '.think-step .btn-add').on('click', '.think-step .btn-add', function()
  69. {
  70. const canAddRows = $(this).data('canAddRows');
  71. const fieldsCount = $(this).data('fieldsCount');
  72. if($('.rows-group').length >= canAddRows) return;
  73. const parentFormGroup = $(this).closest('.form-group');
  74. const $newRow = $('.rows-template').clone();
  75. $newRow.removeClass('rows-template').removeClass('hidden').addClass('rows-group');
  76. $newRow.find('textarea').val('');
  77. parentFormGroup.after($newRow);
  78. $newRow.find('#customFields').trigger("focus");
  79. setLineIndex(canAddRows, fieldsCount);
  80. })
  81. /**
  82. * 删除行。
  83. * Delete rows.
  84. *
  85. * @access public
  86. * @return void
  87. */
  88. $(document).off('click', '.think-step .btn-delete').on('click', '.think-step .btn-delete', function()
  89. {
  90. const canAddRows = $(this).data('canAddRows');
  91. const fieldsCount = $(this).data('fieldsCount');
  92. zui.Modal.confirm({message: $(this).data('deleteTip')}).then((res) => {
  93. if(res)
  94. {
  95. const $row = $(this).closest('.form-group');
  96. $row.remove();
  97. if(canAddRows == 1) $('.add-rows').removeClass('disabled');
  98. setLineIndex(canAddRows, fieldsCount);
  99. }
  100. })
  101. })