样式占位符与best_in_place的值不同

And*_*ank 7 ruby-on-rails-3 best-in-place

我认为我遗漏了一些东西,因为似乎无法通过CSS来区分best_in_place值和占位符(data-nil ="输入你的号码").我只想将占位符设置为与实际值不同,因此它看起来并不令人困惑.

我查看了事件,实际上可以在元素中添加一个类,但是当它们第一次显示在页面上时,没有可用于添加该类的事件.此时我将遍历每个$('best_in_place')并将nil值与span中的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');
    }
  });
Run Code Online (Sandbox Code Playgroud)

小智 6

谢谢你的代码.我解决了这个问题

在我的example.js.erb中

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

  if(element.data('nil') != element.text()) {
    updateBestInPlace($(this));
  }
});
Run Code Online (Sandbox Code Playgroud)

在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');
  }
 }
Run Code Online (Sandbox Code Playgroud)


Pau*_*rth 5

一个非常简单的解决方案是在您的:nil值中包含标记,如下所示:

= best_in_place @user, :email, nil: '<span class="none-yet">None yet!</span>'
Run Code Online (Sandbox Code Playgroud)

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

.none-yet {
  font-style: italic;
}
Run Code Online (Sandbox Code Playgroud)

这对我来说是修改df178520bf9ba405baee9528d5dbb630d6ff760c来自git://github.com/bernat/best_in_place.git