I have 20 check boxes and a submit button on my MVC4 razor page. User can select multiple check boxes(or can leave all of them unchecked) and click on the submit button. How can I get the selected check box values (yes/no) to my controller using a jQuery ajax POST .
我的MVC4剃须刀页面上有20个复选框和一个提交按钮。用户可以选中多个复选框(或者可以不选中所有复选框)并单击提交按钮。如何使用jQuery ajax POST将所选复选框值(是/否)提供给我的控制器。
2 个解决方案
#1
1
If you want to include "no" values as well for checkboxes, you might have to do something a little more hand-rolled; by default, serialize will not include checkbox values for ones that aren't checked, which can be seen in the jQuery documentation.
如果你想在复选框中加入“no”值,你可能需要做一些更多的手工操作;默认情况下,serialize将不包含未检查的复选框值,这可以在jQuery文档中看到。
You could always iterate over the checkboxes with something like
你总是可以用类似的东西遍历复选框
$("input[type='checkbox']")
$( “输入[类型= '复选框']”)
and use a for loop going over each one, including it in your JSON data to post to your server.
并使用for循环遍历每个,包括它在您的JSON数据中发布到您的服务器。
#2
0
In jQuery, serialize() on a form will give you all the values. Then you just set this as the data in your post request.
在jQuery中,表单上的serialize()将为您提供所有值。然后,您只需将其设置为发布请求中的数据。
var attachment = $('form#yourFormId').serialize();
#1
1
If you want to include "no" values as well for checkboxes, you might have to do something a little more hand-rolled; by default, serialize will not include checkbox values for ones that aren't checked, which can be seen in the jQuery documentation.
如果你想在复选框中加入“no”值,你可能需要做一些更多的手工操作;默认情况下,serialize将不包含未检查的复选框值,这可以在jQuery文档中看到。
You could always iterate over the checkboxes with something like
你总是可以用类似的东西遍历复选框
$("input[type='checkbox']")
$( “输入[类型= '复选框']”)
and use a for loop going over each one, including it in your JSON data to post to your server.
并使用for循环遍历每个,包括它在您的JSON数据中发布到您的服务器。
#2
0
In jQuery, serialize() on a form will give you all the values. Then you just set this as the data in your post request.
在jQuery中,表单上的serialize()将为您提供所有值。然后,您只需将其设置为发布请求中的数据。
var attachment = $('form#yourFormId').serialize();