addClass and removeClass not working in IE8

I am trying to validate all teh required classes in a form and then display error message. In that addClass and removeClass is not working in IE8. Any help is appreciated on this. Below is the code

$('#submit_form .required').filter(':visible').each(function () {
    //$(this).next('div.error_text').remove();//Not working in IE8
    //$(this).removeClass('required');//Not working in IE8
    $(this).after('<div class="error_text">Required</div>').remove();
    if (!$(this).val()) {
        //input.addClass('required');//Not working in IE8
        $(this).css("border","1px solid #FF0004");
        $(this).after('<div class="error_text">Required</div>');
        returnVal = false;
    }
    else{
        returnVal = true;
    }
});

Here if the field is not filled, it will create a red box around the field and display message. So if the user submits the form 2 or 3 times, that many times the message is displayed. So I want to remove that message first and then check if the field is empty and correspondingly add required. I tried to use addClass and removeClass and its not working. I am struggling with this since 1 day. Can some body please help me resolving this.....


This code works fine on IE8, you must give us more data on what you are trying to do.

Why are you trying to remove the class just to add it after? why not just remove it if the value is empty? What is your jquery version?

I made a small test and worked just fine on IE8:

<!DOCTYPE html>
<html><head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <script type="text/javascript" src="http://code.jquery.com/jquery-git.js"></script>
  <style type="text/css">
    .required {border:red solid 1px}
  </style>



<script type="text/javascript">//<![CDATA[ 
$(window).load(function(){
$('#submit_form .required').filter(':visible').each(function () {
    var $this = $(this);
    $this.next('div.error_text').remove();//Not working in IE8
if (!$this.val()) {
    $this.after('<div class="error_text">Required</div>');
    returnVal = false;
}
else{
    $this.removeClass('required');//Not working in IE8
     returnVal = true;
}
});
});//]]>  

</script>


</head>
<body>
  <form id="submit_form">
    <input type="text" class="required" value="a">

    <br>
    <input type="text" class="required" value="">
    <br>
    <input type="text" class="required" value="">
    <br>
    <input type="text" class="required" value="">
    <br>
    <input type="text" class="required" value="">
    <br>
    <input type="text" class="required" value="">
    <br>
</form>
</body></html>
链接地址: http://www.djcxy.com/p/83150.html

上一篇: 如果选中,则更改标签css类的复选框

下一篇: addClass和removeClass在IE8中不起作用