2011-09-22 31 views
1

嗨即時通訊使用codeigniter來生成一個窗體錯誤只顯示如果有一個錯誤,該字段我使用twitter-bootstrap的樣式,所以我需要一種方法來添加一類的錯誤將div(class =「clear fix」)和輸入if(span class =「help-inline」)作爲子項存在。如果孩子存在,將類添加到家長

寧願不使用查詢,以便正常降解,但任何選項將是一件好事

任何幫助將是巨大的。希望是有道理的。代碼是感謝下面

<?php echo form_open('login'); ?> 
    <fieldset> 
    <div class="clearfix"> 
     <?php echo form_label('Email Address: ', 'email_address');?> 
     <div class="input"><?php echo form_input('email_address', set_value('email_address'), 'id="email_address" class="xlarge" autofocus ');?> 
     <?php echo form_error('email_address', '<span class="help-inline">', '</span>'); ?> 
     </div> 
    </div><!-- /clearfix --> 
    <div class="clearfix"> 
     <?php echo form_label('Password: ', 'password');?> 
     <div class="input"><?php echo form_input('password', '', 'id="password" class="xlarge"');?> 
     <?php echo form_error('password', '<span class="help-inline">', '</span>'); ?> 
     </div> 
    </div><!-- /clearfix --> 
    <div class="actions"> 
     <?php echo form_submit('submit', 'Login', 'class="btn primary"'); ?> 
    </div> 
    </fieldset> 
    <?php echo form_close(); ?> 

呈現的HTML(如果被觸發錯誤)

<form action="http://unetics.site/index.php/login" method="post" accept-charset="utf-8"> 
    <div style="display:none"> 
     <input type="hidden" name="csrf_test_name" value="c0856f469b1f498480881a8512042ccf" /> 
    </div> 
    <fieldset> 
     <div class="clearfix"> 
      <label for="email_address">Email Address: </label> 
      <div class="input"> 
       <input type="text" name="email_address" value="" id="email_address" class="xlarge" autofocus />    
       <span class="help-inline">The Email Address field is required.</span>     </div> 
     </div><!-- /clearfix --> 
     <div class="clearfix"> 
      <label for="password">Password: </label> 
      <div class="input"><input type="text" name="password" value="" id="password" class="xlarge" /> 
       <span class="help-inline">The Password field is required.</span> 
      </div> 
     </div><!-- /clearfix --> 
     <div class="actions"> 
      <input type="submit" name="submit" value="Login" class="btn primary" /> 
     </div> 
    </fieldset> 
</form> 
+0

也不知道這是否改變了任何東西,但我用少生成我的css –

+4

顯示呈現的HTML; php(服務器端)與JavaScript(客戶端)完全無關。 –

回答

3

我建議使用:

$('.help-inline').closest('.clear-fix').addClass('error').end().prev('input').addClass('error'); 

JS Fiddle demo

JS Fiddle demo, with your rendered html(請注意,我已將jQuery closest()更改爲:closest('.clearfix'))。

或者:

$('.clear-fix').each(
    function(){ 
     if ($(this).has('.help-inline')){ 
      $(this).addClass('error'); 
      $(this).find('input').addClass('error'); 
     } 
    }); 

JS Fiddle demo

JS Fiddle demo, with your rendered html(請注意,我已將jQuery選擇器更改爲:$('.clearfix'))。

+0

感謝這偉大使完美的感覺和JS小提琴作品,但不會在我的網站上出於某種原因 –

+0

檢查編輯。在原始演示中,我使用了'clear-fix'而不是'.clearfix'。我已經在新修訂的演示中改變了這一點(除了那些新的鏈接之外)。 –

+0

認爲它正在工作,但現在它突出顯示所有不論孩子(在你的js小提琴中也是這樣) –