通过Request.Form获取同name的checkbox所有值
2013-01-16 17:20
525 查看
前端页面需要有一个动态增加表格行的功能,引用了table.addrow.js这个jquery插件,每一行有一个checkbox,name统一为cbMaintainRatio,而鉴于这部分是一个纯Html Form的提交非用户控件,所以我们在后端使用Request.Form来获取值,但问题出来了:
如果我们有多行表单,也就是有多个name为cbMaintainRatio的checkbox,post到后端的form,我们通过Request.Form["cbMaintainRatio"]只能获取到一个值"on",而不是像<input type="text" name="width" />这种获取到的"100,200,"值。
浏览了一遍addrow插件的文档,他竟然不支持event,好吧...那只能我们自己来改造代码了:
页面增加一个hidden input,目的为保存多个checkbox的值,如果选中则设定为true,否则false,然后用,分割赋值给这个hidden input
提交Form的按钮绑定上面这个js 方法:
OK,这样我们就可以在后台代码通过Request.Form的形式获取到每一行这个name="cbMaintainRatio" checkbox的值了!
<table border="1" class="atable"> <tbody><tr class="evenRow"> <th> width(px) </th> <th> height(px) </th> <th>maintain ratio</th> <th></th> </tr> <tr class="cloneRow9674 oddRow"> <td> <input type="text" size="25" name="imgwidth"> </td> <td> <input type="text" size="25" name="imgheight"> </td> <td> <input type="checkbox" name="maintainratio"> </td> <td class="btnCol"> <input type="button" value="Remove" class="delRow delRow9674" style="display: inline-block;"> </td> </tr><tr class="cloneRow9674 evenRow"> <td> <input type="text" size="25" name="imgwidth"> </td> <td> <input type="text" size="25" name="imgheight"> </td> <td> <input type="checkbox" name="maintainratio"> </td> <td class="btnCol"> <input type="button" value="Remove" class="delRow delRow9674" style="display: inline-block;"> </td> </tr><tr class="cloneRow9674 oddRow"> <td> <input type="text" size="25" name="imgwidth"> </td> <td> <input type="text" size="25" name="imgheight"> </td> <td> <input type="checkbox" name="maintainratio"> </td> <td class="btnCol"> <input type="button" value="Remove" class="delRow delRow9674" style="display: inline-block;"> </td> </tr> <tr class="evenRow"> <td colspan="4"><input type="button" value="Add" class="alternativeRow addRow9674"></td> </tr> </tbody></table>
如果我们有多行表单,也就是有多个name为cbMaintainRatio的checkbox,post到后端的form,我们通过Request.Form["cbMaintainRatio"]只能获取到一个值"on",而不是像<input type="text" name="width" />这种获取到的"100,200,"值。
浏览了一遍addrow插件的文档,他竟然不支持event,好吧...那只能我们自己来改造代码了:
页面增加一个hidden input,目的为保存多个checkbox的值,如果选中则设定为true,否则false,然后用,分割赋值给这个hidden input
function setMaintainRatio() { var fields; $(':checkbox:checked').each(function () { var txt = $("input[name='cbMaintainRatioList']"); fields = ($(':checkbox').map(function () { if (this.checked) return "true"; else return "false"; }).get().join(",")); $(txt).val(fields); }); }
提交Form的按钮绑定上面这个js 方法:
<asp:Button ID="btwImageCreate" runat="server" Text="Image Create" OnClick="btwImageCreate_Click" OnClientClick="setMaintainRatio(); return true" /> <input type="hidden" name="cbMaintainRatioList" />
OK,这样我们就可以在后台代码通过Request.Form的形式获取到每一行这个name="cbMaintainRatio" checkbox的值了!
相关文章推荐
- 通过SpringAOP获取request中所有参数,记录用户操作日志
- jquery获取特定name所有选中的checkbox,支持IE9标准模式
- 用document.forms[formname].elements[elementname].value获取FORM元素的value--测试通过
- SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析
- java后台怎么获取form表单里所有input的name和value
- 后台C#代码获取html控件的值使用的Request.Form["name"] 中间的那个应该是空间的name 而不是id
- 后台C#代码获取html控件的值使用的Request.Form["name"] 中间的那个应该是空间的name 而不是id
- 关于Request.Form获取listbox所有项的问题
- jquery获取特定name所有选中的checkbox,支持IE9标准模式
- 获取Request.Form所有内容
- jquery通过name获取到所有的value并且构造一个数据传到后台
- form提交,能够提交input的值到后台(后台通过空间的name属性,获取参数值),可以提交div的值到后台吗?
- 不通过getElementByName实现获取表单数据 (document.form表单的name值.input输入框的name值)
- 从form表单 获取表单内所有的name和value!!!
- SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析
- 通过Request.Form得到CheckBoxList当前所点选项的索引值
- form 的 encoding设置不当导致在jsp中使用request.getParametre(paraName)无法获取表单提交的值
- jquery获取特定name所有选中的checkbox,支持IE9标准模式
- 通过Request.Form得到CheckBoxList当前所点选项的索引值
- python通过scapy获取局域网所有主机mac地址示例