2012-03-02 63 views
4

在我的Rails 3應用程序中,我刪除了一個<span>,我用它作爲jQuery輸入的佔位符。如果我提交表單並且出現錯誤,則會重新載入表單並顯示錯誤。但是,對於出現錯誤的字段,<span>將不會在輸入上清除,因爲<span>不在包含輸入的<div>之內。 (這是由於從錯誤中添加了field_with_errors <div>)。我該如何解決這個問題,儘管使用了field_with_errors,仍然會刪除<span>由於Rails 3在字段輸入中刪除跨區域`field_with_errors`

這裏是我的jQuery:

// placeholder label for text 
$(function() { 
    $("span.holder + input").keyup(function() { 
     if($(this).val().length) { 
      $(this).prev('span.holder').hide(); 
     } else { 
      $(this).prev('span.holder').show(); 
     } 
    }); 
    $("span.holder").click(function() { 
     $(this).next().focus(); 
    }); 
}); 

我的HTML與field_with_errors(jQuery的不工作):

<div class="holds email"> 
    <span class="holder">Email</span> 
    <div class="field_with_errors"> 
    <input autocomplete="off" id="user_email" name="user[email]" size="38" type="text" value=""> 
    </div> 
</div> 

我的HTML不field_with_errors(jQuery的作品):

<div class="holdsname"> 
    <span class="holder">Last name</span> 
    <input autocomplete="off" class="inner" id="user_profile_attributes_last_name" name="user[profile_attributes][last_name]" size="18" type="text"> 
</div> 

回答

1

什麼

$('.holds').find('input').keyup(function() 
{ 
    if ($(this).val().length) 
    { 
     $(this).closest('.holds').find('.holder').hide(); 
    } 
    else 
    { 
     $(this).closest('.holds').find('.holder').show(); 
    } 
}); 

$('span.holder').click(function() 
{ 
    $(this).parent().find('input').focus(); 
}); 

jsFiddle

+0

這作品在小提琴肯定的,但不是在我的應用程序的某些原因。 – tvalent2 2012-03-02 03:52:11

+0

啊,複製了錯誤的文字。 jsFiddle有'持有'和'持有人'的類,但答案不是。應該早點抓住了。謝謝!! – tvalent2 2012-03-02 04:18:35

+0

啊,哎呀。我已經更新了匹配的答案。對於那個很抱歉。 – SenorAmor 2012-03-02 11:50:52