use jQuery to get values of selected checkboxes

后端 未结 12 2679
旧巷少年郎
旧巷少年郎 2020-11-27 14:44

I want to loop through the checkboxgroup \'locationthemes\' and build a string with all selected values. So when checkbox 2 and 4 are selected the result would be: \"3,8\"

12条回答
  •  失恋的感觉
    2020-11-27 15:32

    So all in one line:

    var checkedItemsAsString = $('[id*="checkbox"]:checked').map(function() { return $(this).val().toString(); } ).get().join(",");
    

    ..a note about the selector [id*="checkbox"] , it will grab any item with the string "checkbox" in it. A bit clumsy here, but really good if you are trying to pull the selected values out of something like a .NET CheckBoxList. In that case "checkbox" would be the name that you gave the CheckBoxList control.

提交回复
热议问题