简体   繁体   中英

Get parent checkbox value if its parent is not checked

I would like to get the value of the parent checkbox ONLY if it is checked and its parent checkbox is not. For example, If i check Manager 1, I should only get Manager 1. Same goes with the President. But if I check Assistant Manager 1 to 3, I should only get Assistant Manager 1 to 3.

I have this JS but I'm stuck with this line if($(txtbox).is(':checked')==false). It always returns true even though the parent is not checked.

Here is the JS snippet:

$('#xxx').on('click',function () { 
     var hid = new Array();
     var counter = 0;    

     $('ul li input[type=checkbox]').each(function () {   
         if($(this).is(':checked')){   
           var chk = $(this);
            li = chk.closest('li');
            ul = li.parent();   
            parentli = ul.parent();

            if($(parentli).prop("tagName")=="LI"){
                txtbox = parentli.find('input');
                alert($(txtbox).is(':checked'));
                if($(txtbox).is(':checked')==false){
                     hid[counter] = chk.val();
                     counter++;

               }
            }else{
                hid[counter] = chk.val();
            }
     }
  });

});

Here is the html:

<ul>
  <li><label><input type="checkbox" value="Administration" />Administration</label>
    <ul>
        <li><label><input type="checkbox" value="President" />President</label>
            <ul>
                <li><label><input type="checkbox" value="manager 1" />Manager 1</label>
                    <ul>
                        <li><label><input type="checkbox" value="asst manager 1"/>Assistant Manager 1</label></li>
                        <li><label><input type="checkbox" value="asst manager 2"/>Assistant Manager 2</label></li>
                        <li><label><input type="checkbox" value="asst manager 3"/>Assistant Manager 3</label></li>
                    </ul>
                </li>
                <li><label><input type="checkbox" value="manager 2"/>Manager 2</label></li>
                <li><label><input type="checkbox" value="manager 3"/>Manager 3</label></li>
            </ul>
        </li>
        <li><label><input type="checkbox" value="Vice President"/>Vice President</label>
            <ul>
                <li><label><input type="checkbox" value="Manager 4"/>Manager 4</label></li>
                <li><label><input type="checkbox" value="Manager 5"/>Manager 5</label></li>
                <li><label><input type="checkbox" value="Manager 6"/>Manager 6</label></li>
            </ul>
      </li>
   </ul>
  </li>
</ul>

<input type="button" id="xxx"  value="click" />

DEMO: http://jsfiddle.net/Er6Rx/1/

Use:

$(txtbox).prop('checked')

Instead of

$(txtbox).is(':checked')

I'm not sure what it is you're doing with all this code but from what I read it sounds like this is what you want:

http://jsfiddle.net/Er6Rx/8/

$('#xxx').on('click',function(){
      var selected = '';
    $('input[type=checkbox]').each(function () {

          var sThisVal = (this.checked ? "1" : "0");
                         if(sThisVal==true)
                         {
                                 selected += '<li>'+$(this).attr('value')+'</li>';
                         }
                            });

    $('.checkedboxes').append('<ul>'+selected+'</ul>');

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM