【问题标题】:Only first dynamically generated select box is created仅创建第一个动态生成的选择框
【发布时间】:2013-10-23 13:00:17
【问题描述】:

我认为有以下代码:

<% @games.each_with_index do |game, i| %>
  <div class="game_<%= i %> game_hide hide" >
    <% options = options_from_collection_for_select(Player.where("game_id = ?", game.id), 'id', 'handle' ) %>
    <%= f.select(:player_ids, options, {:include_blank => true}, {id: "game_#{i}", :multiple => true} ) %>
  </div>
<% end %>

生成以下html:

<div class="add-players">
  <div class="game_0 game_hide hide" >
  <input name="team[team_divisions_attributes][0][player_ids][]" type="hidden" value="" />
    <select id="game_0" multiple="multiple" name="team[team_divisions_attributes][0][player_ids][]"><option value=""></option>
      <option value="2551">Näryt</option>
      <option value="2552">BrTT</option>
      <option value="2553">Danagorn</option>
      ...
    </select>
  </div>
  <div class="game_1 game_hide hide" >
    <input name="team[team_divisions_attributes][0][player_ids][]" type="hidden" value="" />
    <select id="game_1" multiple="multiple" name="team[team_divisions_attributes][0][player_ids][]"><option value=""></option>
      <option value="4885">Zium</option>
      <option value="4886">Abver</option>
      <option value="4887">Xenocider</option>
      ...
    </select>
  </div>
  <div class="game_2 game_hide hide" >
    <input name="team[team_divisions_attributes][0][player_ids][]" type="hidden" value="" />
    <select id="game_2" multiple="multiple" name="team[team_divisions_attributes][0][player_ids][]"><option value=""></option>
      <option value="4865">Odin</option>
      <option value="4866">Nazgul</option>
      <option value="4867">Dragon</option>
      ...
    </select>
  </div>
</div>

并与以下 jQuery 一起放置:

//when box is checked...
$('.show-tabs .show-tab').live('click', function(){
  var tab = $('.tabs .tab:nth-of-type(' + ( $(this).index() + 1 ) + ')');
  var content = $('.tab-content:nth-of-type(' + ( $(this).index() + 1 ) + ')');
  var destroy_field = content.find('input#team_team_divisions_attributes_' + $(this).index() +'__destroy');
  if ($(this).find('.checkbox').hasClass('checkBoxed')){
    //make it visible
    destroy_field.val('0');
    //shows the game div for the selected game
    $(".game-"+ $(this).index()).show();
    //what's the index?
    alert($(this).index());
    //show the associated tab
    tab.show();
    tab.click();
  } else {
    destroy_field.val('1');
    tab.hide();
    if (tab.hasClass('selected')) {
      $('.tabs .tab:visible').first().click();
      content.hide();
    }
  }
} );

一切都在“那里”——我可以在 Chrome 中的开发者工具页面上找到它,但选择框只在第一个游戏中显示 (game_0)——因此,我认为有一些id 有点问题,但我不知道它是什么。

【问题讨论】:

  • 问题出在index() 方法上。 $(this).index() 将始终返回 0。如果您能告诉我您要将此代码添加到的事件是什么,那么我可以在代码级别为您提供帮助。 :)
  • 我会更新我的帖子,但你说的不是真的 ;) alert($(this).index()); 返回 012,具体取决于我选中的框
  • 你能不能试试$('.show-tabs .show-tab').index($(this))
  • $(".game-"+ $('.show-tabs .show-tab').index($(this))).show(); 代替 $(".game-"+ $(this).index()).show();。请注意$(".game-" 中有-。而你的代码中有_(下划线)。
  • 啊,对,我在阅读 Sandeep Nayak 的回答后在我的代码中添加了-,但没有在问题中更改它们,我的错。让我试试看!

标签: jquery css ruby-on-rails ruby-on-rails-3


【解决方案1】:

问题似乎与.index() 方法的使用有关。如果在没有任何参数的情况下调用该方法,那么它将返回一个整数,即基于兄弟元素的元素的位置。所以在这种情况下$(this).index() 以某种方式返回 0。所以,我建议使用同一方法的不同变体

替换这一行

$(".game-"+ $(this).index()).show();

$(".game-"+ $('.show-tabs .show-tab').index($(this))).show();

现在,它将根据与$('.show-tabs .show-tab')选择器匹配的所有元素返回当前元素的索引或位置。

【讨论】:

    【解决方案2】:

    我认为是因为在 id 和 class 中使用了下划线 (_)。这就是我发现的。可能有点用处。

    https://developer.mozilla.org/en/docs/Underscores_in_class_and_ID_Names

    【讨论】:

    • 有趣,不知道它的历史。谢谢!
    【解决方案3】:

    首先,您在 jQUERY 中有一个错误。您隐藏了div,但您试图显示select 元素。将其更改为:

    //if this thing happens
    //hides all game divs
    $(".game-hide").hide();
    //shows the game div for the selected game with good index method
    $(".game-"+ $('.show-tabs .show-tab').index($(this))).parent('.game-hide').show();
    

    解释:

    $(this)$('.show-tabs .show-tab').live('click' 相关。这是您单击的当前选项卡。因此,要检索$(this) 的索引,您必须与所有$('.show-tabs .show-tab') 元素进行比较。更多解释here

    【讨论】:

    • 我认为您对 .game_hide div 无法正常工作是正确的,但那没有用。感谢您的回答
    • 我已经更新了我的答案,以展示 .index() 的工作原理。 (不正确,因为我不知道 $(this) 是什么!
    • 对不起...我可能只是在愚蠢,但all_this_elements_selector 应该是什么? game-hide/game-x 的父级?我试过了(它是add-player)但一无所获
    • 在您的情况下,它将是$('.show-tabs .show-tab')。看我的帖子,我改了!
    • 那没有做到。不过,我很感谢您的帮助,如果可以的话,我会再给您 +1
    猜你喜欢
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 2015-02-11
    • 2022-11-17
    • 2020-06-11
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    相关资源
    最近更新 更多