2013-02-28 55 views
0

我有复选框列表,如果选择了孩子,父母会自动选择,然后所有选择都会显示在textarea中。即使只有一个孩子被选中,jquery:在textarea中显示父母

问题:

当只有一个孩子被选中,父母没有在文字区域显示。当超过1个孩子被选中时显示。我需要的是家长将上哪怕只有一个孩子被选中

代码的textarea显示:http://jsfiddle.net/NVhnw/

HTML:

  <ul class="taglist"> 
      <li class="category">Category: 
       <ul>     
        <li class="category"><input type="checkbox" name="A" value="Parent">Parent 
          <ul>  
           <li><input type="checkbox" class="liChild" name="A" value="Child1">Child1</li>  
           <li><input type="checkbox" class="liChild" name="A" value="Child2">Child2</li>  
          </ul> 
        </li> 
       </ul> 
      </ul>   
      <br/> 
      You checked: 
      <br/> 
      <textarea rows="4" cols="50" class="textfield" name="describe" class="required" id="describe"> 
      </textarea> 

的Jquery:

   //* Parent select if children selected*// 

       $(document).ready(function() { 
       $('input.liChild').change(function() { 
        $(this).closest('ul') 
          .siblings('input:checkbox') 
          .prop('checked', $(this).closest('ul') 
                .children() 
                .children('input:checkbox') 
                .is(':checked')) 
          .first().change(); 
       }); 


       $('li.category').addClass('plusimageapply'); 
       $('li.category').children().addClass('selectedimage'); 
       $('li.category').children().hide(); 
       $('li.category').each(
       function(column) { 
       $(this).click(function(event){ 
       if (this == event.target) { 
       if($(this).is('.plusimageapply')) { 
       $(this).children().show(); 
       $(this).removeClass('plusimageapply'); 
       $(this).addClass('minusimageapply'); 
       } 
       else 
       { 
       $(this).children().hide(); 
       $(this).removeClass('minusimageapply'); 
       $(this).addClass('plusimageapply'); 
       } 
       } 
       }); 
       } 
       ); 
       }); 

       //*Text area update*// 
       function updateTextArea() {  
        var allVals = []; 
        $('.taglist :checked').each(function(i) { 

         allVals.push((i!=0?"\r\n":"")+ $(this).val()); 
        }); 
        $('#describe').val(allVals).attr('rows',allVals.length) ; 

        } 
        $(function() { 
         $('.taglist input').click(updateTextArea); 
         updateTextArea(); 
       }); 

回答

0

更改此:

$('input.liChild').change(function() { 

$('input.liChild').click(function() { 

这工作了我对你的小提琴

+0

非常感谢您! – Christine 2013-02-28 08:16:50

+0

超级,很高兴我可以帮助! – 2013-02-28 08:51:05