I have a form on my page and am dynamically adding controls to the form with Javascript/JQuery. At some point I need to get all the values in the form on the client side as a collection or a query string. I don't want to submit the form because I want to pass the form values along with other information that I have on the client to a back-end WCF/Ajax service method. So I'm trying to figure out how to capture all the values in the same type of collection that the form would normally send to the server if the form was actually submitted. I suspect there is an easy way to capture this, but I'm stumped.

推荐答案

The jquery form plugin offers an easy way to iterate over your form elements and put them in a query string. It might also be useful for whatever else you need to do with these values.

var queryString = $('#myFormId').formSerialize();

http://malsup.com/jquery/form

或者直接使用jQuery:

var queryString = $('#myFormId').serialize();

Jquery相关问答推荐

使用 shell 脚本判断 json 数组响应是否具有特定用户名和状态的 jq 命令

ASP.NET MVC - Ajax 将空值传递给控制器

无限滚动jQuery插件

为什么 jQuery 不使用 requestAnimationFrame?

使用 JavaScript/jQuery 重定向到 ASP.NET MVC 中的另一个页面

jQuery - 获取 ajax POST 的表单值

如何使用jquery取消绑定所有事件

具有动态大小图像的马赛克网格 gallery

在javascript中检测按键的最简单方法

jQuery 插件:添加回调功能

点击时保持 Bootstrap 下拉菜单打开

jQuery 如果 div 包含此文本,则替换该部分文本

Jquery:如何判断元素是否具有某些 css 类/样式

使用 jQuery 的 ajax 方法将图像作为 blob 检索

用作 Google Chrome 书签

使用 jQuery 的 removeAttr 删除多个属性

jQuery attr 与props ?

表单 propType 失败:您在没有 `onChange` 处理程序的情况下向表单字段提供了 `value` props

将变量的值复制到另一个

如何使用 JQuery 删除onclick?