0

我有下一个 jQuery 代码:

function getParameter() {
    var valor = [];
    $("input[name='TiposServicioSeleccionados']:checked").each(function (i) {
        valor.push($(this).val());
    });
    if (valor.length == 0) {
        valor.push("0");
    }

    return {
        nofacturados: $("#nofacturados:checked").val(),
        precioactual: $("#precioactual:checked").val(),
        TiposServicioSeleccionados: valor
    };
}

我想将 'valor' 授予 'TiposServicioSeleccionados',在我的控制器中我有这个

public ActionResult LeerExt_DevolucionRepuesto(string[] TiposServicioSeleccionados, bool? facturados, bool? nofacturados)
        {
            int id_empresaservicio = Convert.ToInt16(Session["id_empresaservicio"]);
            var res = GetDevolucionRepuestos(TiposServicioSeleccionados,facturados,nofacturados);

            return Json(res.ToDataSourceResult(request));
        }

但TiposServicioSeleccionados总是为空,我不知道为什么......

我打电话getParameter

Read(read => read.Action("LeerExt_DevolucionRepuesto", "Consultas").Data("getParameter"))

我需要,例如,如果我选中了三个复选框,TiposServicioSeleccionados我想保存下一个值:TiposServicioSeleccionados[0]="value of the first chekbox", TiposServicioSeleccionados[1]="value of the second chekbox",TiposServicioSeleccionados[2]="value of the third chekbox"

问候

4

2 回答 2

0

尝试JSON.stringify使用 push 来添加数组对象

function getParameter() {
 var valor = [];
 $("input[name='TiposServicioSeleccionados']:checked").each(function (i) {
    valor.push($(this).val());
 });
 if (valor.length == 0) {
    valor.push("0");
 }

return {

    TiposServicioSeleccionados: JSON.stringify(valor)
};

}

于 2013-11-07T16:18:52.443 回答
0

Your code works just fine, take a look at this fiddle:
http://jsfiddle.net/tXAn6/

I added 3 checkboxes and manually selected the first and the third one.

... value="Servicio A" checked /> A
... value="Servicio B" /> B
... value="Servicio C" checked /> C

The result is:

Servicio A, Servicio C

Edit:

TiposServicioSeleccionados should not be Capitalize, as it is a variable it should start with downcase tiposServicioSeleccionados.

By convection: functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, and SYMBOLIC_CONSTANTS_LIKE_THIS.

于 2013-11-07T16:31:14.647 回答