开发者

jQuery: grabbing form 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜