You need to enable JavaScript to run this app. JS/jQuery实现LGPD同意复选框提交前必填校验求助
优惠活动
大模型
产品
解决方案
定价
更多

JS/jQuery实现LGPD同意复选框提交前必填校验求助

自定义LGPD同意复选框必填校验解决方案

问题根因

  • 现有自定义复选框仅通过CSS类checked标记选中状态,未关联原生表单元素,你用到的Parsley校验库无法识别其选中状态
  • 未实现错误提示的渲染逻辑,配置的校验规则未和组件选中状态做绑定

修复方案

第一步:修改CheckBoxCustom组件代码

function(require,module,exports){
  var CheckBoxCustom,
    bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  CheckBoxCustom = (function() {
    function CheckBoxCustom(config) {
      this._handlerCheckbox = bind(this._handlerCheckbox, this);
      this._options = {
        text: config.text,
        container: config.container,
        "class": config["class"],
        checked: config.checked,
        required: config.required || true,
        message: config.message || 'É necessário concordar com nossos termos de uso.',
        name: config.name // 新增字段,绑定原生表单name属性
      };
      this._externalContainer = this._options.container;
      this._checkBoxContainer = void 0;
      this._create();
    }

    CheckBoxCustom.prototype._create = function() {
      this._checkBoxContainer = $('<div/>').addClass(this._options["class"]);
      this._externalContainer.append(this._checkBoxContainer);
      
      // 新增隐藏原生复选框,用于表单校验识别,也会跟随表单正常提交参数
      this._nativeInput = $('<input type="checkbox" hidden name="' + this._options.name + '">');
      this._checkBoxContainer.append(this._nativeInput);
      
      this._span = $('<span/>');
      this._i = $('<i/>');
      this._checkBox = $('<a/>').text(this._options.text).attr({});
      this._span.append(this._i);
      this._checkBox.append(this._span);
      this._checkBoxContainer.append(this._checkBox);
      this._checkBox.on('click', this._handlerCheckbox);
      
      // 新增错误提示容器
      this._errorTip = $('<div class="error-tip" style="color: #f5222d; font-size: 12px; margin-top: 4px; display: none;">' + this._options.message + '</div>');
      this._externalContainer.append(this._errorTip);
      
      if (this._options.checked === true) {
        this._checkBoxContainer.addClass('checked');
        this._nativeInput.prop('checked', true);
      }
    };

    CheckBoxCustom.prototype._handlerCheckbox = function(e) {
      const isChecked = this._checkBoxContainer.toggleClass('checked').hasClass('checked');
      // 同步原生复选框状态
      this._nativeInput.prop('checked', isChecked);
      // 选中时自动隐藏错误提示
      if (isChecked) this._errorTip.hide();
      return isChecked;
    };

    // 新增获取选中状态方法
    CheckBoxCustom.prototype.getChecked = function() {
      return this._checkBoxContainer.hasClass('checked');
    };

    // 新增校验方法,返回校验是否通过
    CheckBoxCustom.prototype.validate = function() {
      const isPass = !this._options.required || this.getChecked();
      this._errorTip.toggle(!isPass);
      return isPass;
    };

    return CheckBoxCustom;

  })();

  module.exports = CheckBoxCustom;
}

第二步:组件初始化+表单提交校验

// 组件初始化
this._checkBox = new CheckBoxCustom({
  name: 'termos',
  text: 'Li e concordo com o tratamento dos meus dados pessoais conforme previsto na Declaração de Privacidade para Website Audi.',
  container: this._formGroupNews,
  "class": 'newsletterCheckbox',
  checked: false,
  required: true,
  message: '自定义错误提示内容'
});

// 表单提交前触发校验
$('你的表单选择器').on('submit', function(e) {
  // LGPD复选框校验不通过则阻止提交
  if (!this._checkBox.validate()) {
    e.preventDefault();
    return false;
  }
  // 其余表单校验逻辑...
}.bind(this))

验证效果

  • 未勾选复选框点击提交:展示自定义错误提示,表单无法提交
  • 勾选复选框后:错误提示自动消失,表单可正常提交,勾选状态会随原生input正常提交到后端

内容的提问来源于stack exchange,提问作者Leonardo Santiago

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 07:54:03