开发者

jQuery: grabbing form values

开发者 https://www.devze.com 2022-12-18 14:39 出处:网络
If form is submitted to a function using onsubmit(), how can I get an array of the values of all checkboxes with name=\'values[]\' ?

If form is submitted to a function using onsubmit(), how can I get an array of the values of all checkboxes with name='values[]' ?

EDIT: originally I mentioned radio buttons, this is wrong - checkboxes is correct.

EDIT: line below only gets the value of the first one, obviously

$("input[name='values[]']:checked").val()

Also开发者_如何学Go, how can I get the number of checkboxes that were checked when the form was submitted?


val returns a single value. To get all values, use map:

var selectedValues = $("input[name^='values']:checked").map(
       function(){ return $(this).val(); }).get();

This returns an array with all selected values. To get a dash-delimited string:

var dashed = selectedValues.join('-');


try

$('#formId').serializeArray();
0

精彩评论

暂无评论...
验证码 换一张
取 消