如何在页面刷新后提交后滚动并将PHP设置的标签颜色设置为红色到ID为div的div?

时间:2014-04-08 12:54:59

标签: javascript jquery

我的问题是当我点击刷新页面时刷新页面它保持在顶部而不滚动到具有id形式的div?

jQuery的:

$(document).ready(function(){
    if($('label').length > 0){
         $('label').each(function(){  
           if($(this).css('color') == 'red'){
            alert('test');
              $('html, body').animate({
                scrollTop: $("form").offset().top
              }, 1000);
           }
         });
    }
});

HTML:

<form enctype="multipart/form-data" method="post" action="" name="ats_form">
  <div id="form" class=" ui-body ui-body-d"> <!-- element where I want to scroll -->
    <fieldset>
      <legend>
         <b>Kontaktní osoba</b>
      </legend>
       <div data-role="fieldcontain">
         <label for="f_FName">Jméno *</label> <!-- if this label is red, php script check input with regular expression when it is not filled or name is not enter properly then it set this label attribute css to color: red; -->
         <input id="f_FName" class="ui-input-text ui-body-d ui-corner-all ui-shadow-inset" type="text" value="" size="30" name="f_FName"> <!-- this is input that php check -->
       </div>
     </fieldset>    
   </div>
   <input class="btnform ui-btn-hidden" type="submit" name="Submit" value="Odeslat" aria-disabled="false">
</form>
如果输入例如为空或具有错误值,则在PHP验证后使用

HTML:     与以前相同的结构       Jméno*     与以前相同的结构

1 个答案:

答案 0 :(得分:2)

if ($('label[style*="color:red"]').length > 0) { //If there's a label with style*="color:red"...
    alert('scroll'); //...Alert
    $('html, body').animate({ //..animate to form
        scrollTop: $("form").offset().top
    }, 1000);
};

JSfiddle

将其放入$(document).ready - 事件