![](/img/trans.png)
[英]validator.js with bootstrap how do you know your form is valid on AJAX forms?
[英]how to tweak form validator.js to only send form if checkbox is checked
我使用https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form中的示例創建了一個聯系表單,該表單工作正常。 現在我想添加一個用戶必須檢查的復選框(除了google recaptcha和強制表單字段),如果他想要提交表單。
我想我必須調整validator.js文件,但由於我不知道javascript,我不知道如何編輯現有的驗證器文件。
其他復選框的HTML:
<label>
<input type="chbox" name="chbox" id="chbox"><a href="terms.html">Terms and conditions</a>.
</label>
JS:
function getValue($el) {
return $el.is('[type="checkbox"]') ? $el.prop('checked') :
$el.is('[type="radio"]') ? !!$('[name="' + $el.attr('name') + '"]:checked').length :
$el.val()
}
var Validator = function (element, options) {
this.options = options
this.validators = $.extend({}, Validator.VALIDATORS, options.custom)
this.$element = $(element)
this.$btn = $('button[type="submit"], input[type="submit"]')
.filter('[form="' + this.$element.attr('id') + '"]')
.add(this.$element.find('input[type="submit"], button[type="submit"]'))
this.update()
this.$element.on('input.bs.validator change.bs.validator focusout.bs.validator', $.proxy(this.onInput, this))
this.$element.on('submit.bs.validator', $.proxy(this.onSubmit, this))
this.$element.on('reset.bs.validator', $.proxy(this.reset, this))
this.$element.find('[data-match]').each(function () {
var $this = $(this)
var target = $this.data('match')
$(target).on('input.bs.validator', function (e) {
getValue($this) && $this.trigger('input.bs.validator')
})
})
this.$inputs.filter(function () { return getValue($(this)) }).trigger('focusout')
this.$element.attr('novalidate', true) // disable automatic native validation
this.toggleSubmit()
}
只有填寫了必填字段並且復選框和重新簽名檢查完成后,提交按鈕才會變為可點擊狀態,並且可以提交表單。
哦,我剛剛發現自己,復選框的html是不完整的。 我添加了required="required"
。 完整的行現在是<input type="checkbox" name="chbox" id="chbox" required="required">
現在終於按預期工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.