【问题标题】:Css style checkbox when not checked with jQuery validator未使用 jQuery 验证器检查时的 CSS 样式复选框
【发布时间】:2016-05-02 07:16:35
【问题描述】:

如果单击提交按钮后未选中,我希望我的复选框更改标签颜色。我正在使用 jQuery 验证器。我不想显示标签,我只想将 I Accept 更改为红色。

验证

jQuery.extend(jQuery.validator.messages, {
  required:"This field is required",
  email: "A valid email address is required"
});

复选框

<label for="cbx2">
   <input name="TermsCondition" type="checkbox" value="on" style="float:left;" class="required" id="cbx2">
   <strong>I Accept</strong>
</label>

CSS

label.error {
    font: 0/0 a;
}

【问题讨论】:

    标签: javascript jquery html css checkbox


    【解决方案1】:

    这是使用 css 选择器解决此问题的最简单的 css hack。只需将其添加到您的 CSS 中即可。

      .checkbox.valid ~ strong {
    color:#000!important;
    }
    
    input.error,  .checkbox.error  ~ strong{
        border: 1px dotted red!important;
      color:red!important;
    }
    
    label.error {    	
    		display: none!important;  
    }
      
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.js"></script>
    
    
    	<script>
    	$.validator.setDefaults({
    		submitHandler: function() {
    			alert("submitted!");
    		}
    	});
    
    	$().ready(function() {
    
    
    		// validate signup form on keyup and submit
    		$("#signupForm").validate({
    			rules: {
    				firstname: "required",
    				lastname: "required",
    				username: {
    					required: true,
    					minlength: 2
    				},
    				password: {
    					required: true,
    					minlength: 5
    				},
    				confirm_password: {
    					required: true,
    					minlength: 5,
    					equalTo: "#password"
    				},
    				email: {
    					required: true,
    					email: true
    				},
    				topic: {
    					required: "#newsletter:checked",
    					minlength: 2
    				},
    				agree: "required"
    			},
    			messages: {
    				firstname: "Please enter your firstname",
    				lastname: "Please enter your lastname",
    				username: {
    					required: "Please enter a username",
    					minlength: "Your username must consist of at least 2 characters"
    				},
    				password: {
    					required: "Please provide a password",
    					minlength: "Your password must be at least 5 characters long"
    				},
    				confirm_password: {
    					required: "Please provide a password",
    					minlength: "Your password must be at least 5 characters long",
    					equalTo: "Please enter the same password as above"
    				},
    				email: "Please enter a valid email address",
    				agree: "Please accept our policy",
    				topic: "Please select at least 2 topics"
    			}
    		});
    
    		// propose username by combining first- and lastname
    		$("#username").focus(function() {
    			var firstname = $("#firstname").val();
    			var lastname = $("#lastname").val();
    			if (firstname && lastname && !this.value) {
    				this.value = firstname + "." + lastname;
    			}
    		});
    
    		
    		
    	});
    	</script>
    	
    
    
    <div id="main">
    	
    
    	<form class="cmxform" id="signupForm" method="get" action="">
    		<fieldset>
    			<legend>Validate a complete form</legend>
    			<p>
    				<label for="firstname">Firstname</label>
    				<input id="firstname" name="firstname" type="text">
    			</p>
    			<p>
    				<label for="lastname">Lastname</label>
    				<input id="lastname" name="lastname" type="text">
    			</p>
    			<p>
    				<label for="username">Username</label>
    				<input id="username" name="username" type="text">
    			</p>
    			<p>
    				<label for="password">Password</label>
    				<input id="password" name="password" type="password">
    			</p>
    			<p>
    				<label for="confirm_password">Confirm password</label>
    				<input id="confirm_password" name="confirm_password" type="password">
    			</p>
    			<p>
    				<label for="email">Email</label>
    				<input id="email" name="email" type="email">
    			</p>
    			<p>
    				<label for="agree">Please agree to our policy</label>
    				<input type="checkbox" class="checkbox" id="agree" name="agree">
                  <strong>I Accept</strong>
    			</p>
    		
    		
    			<p>
    				<input class="submit" type="submit" value="Submit">
    			</p>
    		</fieldset>
    	</form>

    【讨论】:

    • 如果它能以最简单的方式解决您的问题,请投票。
    • @Ani Menon 它按预期工作。检查添加的sn-p。
    • 代码不工作。我能够通过简单的输入来做到这一点。但我无法在复选框中做到这一点。
    • 刚刚添加的复选框。见鬼更新的答案。它工作正常。
    【解决方案2】:

    您可以使用 Jquery validate 回调函数 (成功时,显示错误)

    $("#registerForm").validate({
      success: function() {
        $("#L_Accept").css('color', 'black');
      },
      showErrors: function() {
        $("#L_Accept").css('color', 'red');
        this.defaultShowErrors();
      },
      
      /* Disable (focusout, keyup, click) Events
         to force check validation by Submit Button ONLY.
      */
      onfocusout: false,
      onkeyup: false,
      onclick: false
    });
    label.error {
      display:none !important; //hide default error label.
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
    
    <h1>Form Validation Example</h1>
    <form id='registerForm' name='registerForm' method='post' action='' >
      <label for="cbx2">
        <input name="TermsCondition" type="checkbox" value="on" style="float:left;" class="required" id="cbx2">
        <strong id="L_Accept">I Accept</strong>
      </label>
      
      <input type='submit' name='Submit' value='Submit' />
    </form>

    【讨论】:

    • 我希望它在提交按钮提交后变为红色。
    • @Jerika 查看我的更新答案,我希望它能如你所愿,谢谢
    【解决方案3】:

    你可以使用css:not()和参数:checked,相邻兄弟+选择器;在onsubmit 事件中,如果#cbx2 不是checked,则添加className#cbx2 color 设置为red

    document.getElementById("form").onsubmit = function(event) {
      var message = this.querySelector("#cbx2");
      if (!message.checked) message.className = "invalid";
    }
    #cbx2:not(:checked).invalid + strong {
      color: red;
    }
    <form id="form">
      <label for="cbx2">
        <input name="TermsCondition" type="checkbox" value="on" style="float:left;" class="required" id="cbx2">
        <strong>I Accept</strong>
      </label>
      <input type="submit" />
    </form>

    【讨论】:

    • 我希望它在点击提交按钮时变为红色。
    • @Jerika “我希望它在点击提交按钮时变为红色。”查看更新后的帖子。
    猜你喜欢
    • 2017-12-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多