简体   繁体   中英

fetch checked value from multiselect checkbox in jquery

I have given html

<ul class="ui-multiselect-checkboxes ui-helper-reset" style="height: 175px;">
  <li class=" ">
    <label class="ui-corner-all" title="" for="ui-multiselect-opps-option-0">
      <input type="checkbox" title="" value="All" name="opps" id="ui-multiselect-opps-option-0">
      <i>All</i>
    </label>
  </li>
  <li class=" ">
    <label class="ui-corner-all" title="" for="ui-multiselect-opps-option-1">
      <input type="checkbox" title="" value="1" name="opps" id="ui-multiselect-opps-option-1">
      <i>John</i>
    </label>
  </li>
  <li class=" ">
    <label class="ui-corner-all" title="" for="ui-multiselect-opps-option-2">
      <input type="checkbox" aria-selected="true" checked="checked"  title="" value="3" name="opps" id="ui-multiselect-opps-option-2">
      <i>Tim</i>
    </label>
  </li>
  <li class=" ">
    <label class="ui-corner-all" title="" for="ui-multiselect-opps-option-3">
      <input type="checkbox" aria-selected="true" checked="checked" title="" value="2" name="opps" id="ui-multiselect-opps-option-3">
      <i>Tom</i>
    </label>
  </li>
</ul>

and have included given jquery code

$("input[id^='ui-multiselect-opps-option']").attr('checked').val();

but its not fetching checked values please guide me how to fetch multiselect checked values through jquery.

Use .map() in jquery. It is used to translate all items in an array or object to new array of items.

var res = $("input[id^='ui-multiselect-opps-option']:checked").map(function() {
  return $(this).val();
}).get();

 console.log(res);

Fiddle

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