2012-07-28 72 views
7

我想我错过了一些东西,因为似乎没有办法通过CSS区分best_in_place值和占位符(data-nil =“输入您的号码”)。我只想以不同的方式设置占位符,然后设置实际值,所以它看起来并不令人困惑。风格占位符不同于价值与best_in_place

我已经研究过这些事件,并且实际上可以向该元素添加一个类,但是当它们在第一次显示在页面上时,没有可用于添加该类的事件。在这一点上,我将遍历每个$('best_in_place')并将nil值与跨度中的html值进行比较,但在执行此操作之前,我只是想知道是否错过了某些内容。

那么有没有一种方法来区分已设置的值与best_in_place?

如果代码让你更好地理解:这个工作原理,只是向best_in_place添加或删除一个类,但只有在用户编辑该字段时才设置,而不是在初始页面加载时设置。

$('.best_in_place').bind('best_in_place:update', function(evt){ 
    if($(this).data('nil') == $(this).html()){ 
     $(this).removeClass('has_value'); 
    }else{ 
     $(this).addClass('has_value'); 
    } 
    }); 

回答

6

感谢您的代码。我

解决了这个在我example.js.erb

$('.top-column .best_in_place').each(function() { 
    var element = $(this); 

    if(element.data('nil') != element.text()) { 
    updateBestInPlace($(this)); 
    } 
}); 

而且在example.js

$(document).ready(function(){ 
    $('.top-column .best_in_place').live("ajax:success", function(){ 
    updateBestInPlace($(this)); 
    }); 
}); 

function updateBestInPlace(element){ 
    if(element.data('nil') == element.html() || element.html() == ""){ 
    element.removeClass('has_value'); 
    }else{ 
    element.addClass('has_value'); 
    } 
} 
5

一个非常简单的解决方案是包括标记在:nil值,就像这样:

= best_in_place @user, :email, nil: '<span class="none-yet">None yet!</span>' 

然后你就可以有这样的CSS规则:

.none-yet { 
    font-style: italic; 
} 

这是从git的工作对我来说与修订df178520bf9ba405baee9528d5dbb630d6ff760c://github.com/bernat/best_in_place.git

+0

天才!谢谢 :) – 2016-08-01 12:52:25