2013-04-23 67 views
1

我在使用數據標記中的多個值並應用基於此id的過濾器時遇到問題。因爲它在數據標籤中顯示多個ID,所以它不會顯示兩個ID。在http://jsfiddle.net/rWhVN/jquery數據屬性多個值

<script type="text/javascript"> 
     $(function() { 
      $('#content').removeClass('nojs'); 
      $('.row').not('#q1').hide(); 
      $('select').on('change', function() { 
       var question = $(this).parent().parent().attr('id'); 
       var answerID = $(this).children('option:selected').attr('id'); 
       var loadQuestion = $(this).children('option:selected').data('load'); 
       $('#' + question).addClass('answered'); 
       $('.row').not('.answered').hide(); 
       $('#' + loadQuestion).fadeIn(); 
       console.log(loadQuestion); 
      }); 
     }); 
    </script> 

<option id="q1a1" data-load="q2, q8">Answer 1</option> 

問題一個答案

例一應顯示問題,兩個問題八。

不知道你如何分割出來,所以任何幫助表示讚賞。

回答

1

$('#' + loadQuestion)將是$('#q2, q8'),這將不會是#q2#q8的seletor。

你可以做data-load="#q2, #q8",然後只是做$(loadQuestion)

See the demo

如果你不能改變data-load屬性,那麼你可以用做:

$($.map(loadQuestion.split(/ *, */), function(el) {return '#'+el;}).join(',')).fadeIn(); 
+0

這就是真正有用的,我還沒有在地圖功能看起來又是那麼非常感謝您的幫助 – user989952 2013-04-23 00:17:29