使复选框引起jQuery验证 [英] Make checkbox cause jQuery validation

查看:50
本文介绍了使复选框引起jQuery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有一个bootstrap datetimepicker,这是填充页面其余部分的必填字段.

I have and bootstrap datetimepicker in my form that is a required field to populate the rest of my page.

使用提交按钮时,我可以设法引起验证,但是我有一个复选框,使用日期时间选择器中的值填充字段.

I can manage to cause validation when I use a submit button, but I have a checkbox that populates fields with the values from the datetimepicker.

我需要此复选框来导致对datetimepicker的验证而无需提交.

I need the checkbox to cause the validation for the datetimepicker without the submit.

我的代码如下:

//My datepicker
<div id="dtpDate" class="input-append" >
    <input required data-format="yyyy-MM-dd" type="text" style="width: 75%;" />
    <span class="add-on">
        <i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
    </span>
</div>

//Checkbox to cause validation
<div>
    <input type="checkbox" data-bind="checked: setdate, click: setdates"/>
</div>


//My Script
$(document).ready(function () {
    $("form").validate();
});

$(function () {
    $('#dtpDate').datetimepicker({
        pickTime: false
    })
});

如何使复选框引起验证? (不提交)

How do I make the checkbox cause validation? (Without submit)

预先感谢

推荐答案

使用 .valid() 单击复选框以验证文本字段.

Use .valid() to validate the text field upon click of the checkbox.

jQuery :

$(document).ready(function () {

    $("form").validate({
        // your options & rules
    });

    $('#mycheck').click(function () {
        $('#myfield').valid();
    });

});

HTML :

<form>
    <input required="required" type="text" name="myfield" id="myfield" />
    <input type="checkbox" name="mycheck" id="mycheck" />
    ...
</form>

工作示例: http://jsfiddle.net/rzjRM/

Working Demo: http://jsfiddle.net/rzjRM/

这篇关于使复选框引起jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆