Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Select with checkbox (https://javascript.ru/forum/misc/48634-select-checkbox.html)

Gasherez 10.07.2014 18:55

Select with checkbox
 
Здравствуйте! Имеется такая разметка:
<div class="select-wishes">
<div class="selected-items"></div>
<ul>
<li>
<label for="with-conditioning">
<input type="checkbox" name="" id="with-conditioning"> Кондиционер
</label>
</li>
<li>
<label for="with-luggage">
<input type="checkbox" name="" id="with-luggage"> С собой большой багаж
</label>
</li>
<li>
<label for="with-pet">
<input type="checkbox" name="" id="with-pet"> С животным
</label>
</li>
<li>
<label for="with-smoking">
<input type="checkbox" name="" id="with-smoking"> Курить в машине
</label>
</li>
</ul>
</div>

ul спрятан. Нужен эффект селекта. При открытии выбираем несколько чекбоксов, текст который в теге label записывается в <div class="selected-items">. Подскажите пожалуйста кто знает как решить эту задачку. Я такое делал с обычным текстом методом $().text Тут не знаю как начать.

рони 10.07.2014 19:39

Gasherez,
обойти все input и если выбран сохранить текст потом переменную со всеми выбранными текстами вывести в див

Gasherez 10.07.2014 20:08

А как их обходить если они в label, а label в li

рони 10.07.2014 20:24

Gasherez,
:cray:
<!DOCTYPE HTML>

<html>

<head>
  <title>Untitled</title>
  <meta charset="utf-8">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script>
     $(function(){
       var s = $(".select-wishes input:checked"), arr = [];
       s.each(function(indx, el){
         var text = $("[for='"+el.id+"']").text();
         arr.push($.trim(text))});
      alert(arr)
});
  </script>
</head>

<body>
<div class="select-wishes">
 <div class="selected-items"></div>
 <ul>
 <li>
 <label for="with-conditioning">
 <input type="checkbox" name="" id="with-conditioning"> Кондиционер
</label>
 </li>
 <li>
 <label for="with-luggage">
 <input type="checkbox" name="" id="with-luggage" checked="checked"> С собой большой багаж
</label>
 </li>
 <li>
 <label for="with-pet">
 <input type="checkbox" name="" id="with-pet" checked="checked"> С животным
</label>
 </li>
 <li>
 <label for="with-smoking">
 <input type="checkbox" name="" id="with-smoking"> Курить в машине
</label>
 </li>
 </ul>
 </div>

</body>

</html>

Gasherez 10.07.2014 21:03

Спасибо. Будем пробовать.


Часовой пояс GMT +3, время: 15:21.