【问题标题】:Make a validation with checkbox使用复选框进行验证
【发布时间】:2012-10-21 19:51:13
【问题描述】:

我是 JavaScript 新手。我想对我的复选框进行验证。我有这个html

<input type="checkbox" name="acceptTerms"/>
<input type="submit" name="subscribe" value="Enter now" title="Enter now" />

当您提交时,您没有选中复选框。复选框必须获得类“错误”。当您选中复选框时。比形式必须是 sumit。用 jQuery / js 怎么做?

谢谢

【问题讨论】:

    标签: javascript jquery validation


    【解决方案1】:

    尝试以下方法:

    $('#yourForm').submit(function(e){
        var $check = $('input[name=acceptTerms]');
        if (!$check.is(':checked')) {
           $check.addClass('error');
           e.preventDefault()
        }
    })
    
    $('input[name=acceptTerms]').change(function(){
         if (this.checked) $(this).removeClass('error')
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-01
      • 2017-04-03
      • 2023-04-05
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 2020-07-12
      相关资源
      最近更新 更多