简体   繁体   English

在html表单中填写表格时,如何使自定义CSS代码无效?

[英]How can I make the custom css code for invalid values while filling the form in html?

I'm using the plugin from http://jqueryvalidation.org/ to validate my form. 我正在使用http://jqueryvalidation.org/中的插件来验证我的表单。 The html code is simple: html代码很简单:

<form id="myform">  
     <input type="text" name="field1" />  <br/>  
    <textarea maxlength="140" style="resize:none" class="textoxarea" placeholder="zbzbzxcbcx" name="field2"></textarea> 
    <input type="checkbox" name="terms" />x
     <input type="submit" />
</form>

and the javascript is: 和JavaScript是:

$(document).ready(function() {

    $('#myform').validate({ // initialize the plugin
        errorElement: 'div',
        rules: {
            field1: {
                required: true,
                email: true
            },
            field2: {
                required: true,
                minlength: 5
            },
            terms: {
                required: true,
                maxlength: 2
            }
        },
        submitHandler: function(form) { // for demo
            alert('valid form submitted'); // for demo
            return false; // for demo
        }
    });

});

It works, as you can see in my fiddle , but I would like to achieve slightly different effect. 正如您在我的小提琴中看到的那样,它可以正常工作,但是我想获得稍微不同的效果。 I would like to - instead of showing the error message below the fields, just change their css style, so that when the value doesn't meet its requirements - each component will get the small red border (1px). 我想-而不是在字段下方显示错误消息,只需更改其CSS样式,以便当该值不符合其要求时-每个组件都会获得小的红色边框(1像素)。 Btw, is there a way to make the button active only when the three options above are filled properly? 顺便说一句,有没有一种方法可以使按钮只有在正确填写以上三个选项后才能激活? Thanks! 谢谢!

Simply use the CSS: 只需使用CSS:

.textox.error, .textoxarea.error,
.textox.error:focus, .textoxarea.error:focus{
    border:1px solid red; /* add the border to your input */
}
.error[generated=true]{
    display:none!important; /* hide the error message, overriding the inline styling */
}

Demo Fiddle 演示小提琴

To make the button active only when the form is valid, disable it by default: 要使按钮仅在表单有效时才激活,默认情况下禁用它:

<input type="submit" disabled />

Then, check validation of the form when an input value changes, and enable if valid: 然后,在输入值更改时检查表单的有效性,并在有效时启用:

$('#myform').find('input, textarea').on('change', function () {
    var btn = $('#myform').find('input[type=submit]');
    if ($('#myform').valid()) {
        btn.removeAttr('disabled');
    } else {
        btn.attr('disabled', 'disabled');
    }
});

Demo Fiddle 演示小提琴

Add the error placement handler in the validate function like below- 将错误放置处理程序添加到validate函数中,如下所示:

errorPlacement: function (error, element) {
}

You can handle how and where the error text label should be displayed or not be displayed at all, if you don't specify in this function callback. 如果未在此函数回调中指定,则可以处理应该显示或完全不显示错误文本标签的方式和位置。 In this way the label text will not exist in the DOM. 这样,标签文本将不会在DOM中存在。

And then add the css for the "error" class which is added to the control by the validator plugin like this- 然后为“错误”类添加css,该类由验证器插件添加到控件中,如下所示:

.error{
    border: 1px solid red !important;
}

Update: 更新:
The complete code for the cleaner way to handle your requirements- 完整的代码,可以更清晰地处理您的要求-

$(document).ready(function () {
    $('input[type="submit"]').attr('disabled', 'disabled');
    var validator = $('#myform ').validate({ // initialize the plugin
        rules: {
            email: {
                required: true,
                email: true
            },
            field2: {
                required: true,
                minlength: 5
            },
            terms: {
                required: true,
                maxlength: 2
            }
        },
        errorPlacement: function (error, element) {},
        success: function () {
            if(validator.numberOfInvalids() == 0)
            {
                $('input[type="submit"]').removeAttr('disabled');
            }
            else
            {
                $('input[type="submit"]').attr('disabled', 'disabled');
            }
        },
        submitHandler: function (form) { // for demo
            alert('valid form submitted '); // for demo
            return false; // for demo
        }
    });
    validator.form();

    $('input[type="checkbox"]').on('click',function(){
        validator.form();
    });
});

Fiddle Url- http://jsfiddle.net/xvAPY/228/ 小提琴网址-http: //jsfiddle.net/xvAPY/228/

You only need this: It will also help customize error message. 您只需要这样做:它也将有助于自定义错误消息。

input.error, textarea.error, input.error:focus, textarea.error:focus{
  border:1px solid red;
}
.error[generated=true]{
  color: red;
}

Here's a DEMO 这是一个演示

Sounds like you want the highlight option? 听起来像您想要突出显示选项?

$(".selector").validate({
  highlight: function(element, errorClass, validClass) {
$(element).addClass(errorClass).removeClass(validClass);
$(element.form).find("label[for=" + element.id + "]")
  .addClass(errorClass);
  },
  unhighlight: function(element, errorClass, validClass) {
$(element).removeClass(errorClass).addClass(validClass);
$(element.form).find("label[for=" + element.id + "]")
  .removeClass(errorClass);
  }
});

Then add .errorClass to your CSS and define what you want it to look like. 然后将.errorClass添加到CSS并定义您想要的外观。

All options can be found here: http://jqueryvalidation.org/validate 所有选项都可以在这里找到: http : //jqueryvalidation.org/validate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将HTML表单的值转换为“自定义” URL? - How can I get an HTML form's values into a “custom” URL? 如何制作带有自定义复选框的表单,该复选框会将我定向到提交表单上的不同html文件? - How can I make a form with custom check boxes that will direct me to different html files on submit form? 如何在 HTML/CSS 中制作计数器 - How can I make a counter in HTML/CSS 如何在 javascript 中制作 css 代码? - How can I make the css code in javascript? 我如何制作一个输入类型的提交按钮,填写表格后才能使用 - How can i make a input type submit button that will just can used after filling up the form 如何根据填写 html 表单使页面区域可见? - How to make an area of a page visible based on filling in an html form? 如何将重复的CSS值存储在变量中,以减少代码的重复性? - How can I store the repeating css values in varialbles to make the code less repetitive? 在HTML表单中,我们可以在用户填写表单时验证文本字段吗 - In HTML form, can we validate the text fields while the user is filling in the form 我可以制作一个我可以在CSS,JS和HTML之间共享的自定义颜色定义吗? - Can I make a custom colour definitions that I can share between CSS, JS and HTML? 如何在 Antora 中包含自定义 CSS、HTML 和 Javascript? - How can I include custom CSS, HTML and Javascript in Antora?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM