如何添加条件到空白select2选项? :include_blank =>真正?

时间:2015-05-24 17:23:34

标签: jquery ruby-on-rails jquery-select2 ui-select2 select2-rails

当select选项是空白占位符时,如何找到它,以便我可以执行相应的jquery / javascript?

这是我的标记(haml):

= form_tag(compare_path, method: 'get', remote: true, id: 'compare_form') do
          = select_tag "votus_friend_id", options_from_collection_for_select(@votus_friends, :id, :full_name, @comparison.votus_friend.id), :include_blank => true, :class => "compare-search", :style => "width:100%; margin-left: 3px;"

这是我的JS。我知道它离开这里了。我似乎无法找到从select2返回的空白占位符的确切值,以便运行条件:

$('.compare-search').on('change', function() {

  console.log('thisss', $('<select/>')  );

  if ($('.compare-search').val() === '') {
    $('.vScoreExplain').show();
    $('.stats').hide();
  }
  else {
    $('.vScoreExplain').hide();
    $('.stats').show();
  }
});

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

$('.compare-search').on('change', function() {

  if ($(this).val().length) {
    $('.vScoreExplain').hide();
    $('.stats').show();
  }
  else {
    $('.vScoreExplain').show();
    $('.stats').hide();
  }
});