5

我的应用程序在 MVC 3 中,我正在使用 razor 引擎,我试图在 web 网格中选择第一个项目(默认突出显示的复选框),它没有在 IE7 中被选中,这里是代码:

$("#@item").change(function () {

});
4

1 回答 1

0

好的...让我们一步一步来...因为您没有提供太多数据...我将为您制作一个场景:

假设你有这个复选框:

@Html.CheckBoxFor(model=>model.isValid,new {id="chkValid"})

当然 ISValid 是一个布尔变量。

一种简单的方法是这样做:

@Html.CheckBoxFor(model=>model.isValid,new {@checked = "checked"})

现在,如果由于某种原因不起作用......你总是可以采用老式的方式...... Jquery:在准备好的文档中......这样做:

 $(document).ready(function () {
        $('chkValid').attr('checked')= true;
    });

希望能帮助到你


稍后编辑:好的......试试这个:

@Html.CheckBoxFor(model=>model.isValid,new {id="chkValid" , onclick="updateChk("+@model.parameter+")"}) // the value you need to send to the controller 

这是jquery函数:

  function updateChk(parameter)
    {
            if (($('#chkValid').is(':checked')) {
                var check = true;
            } else {
                check = false;
            }
    //see if checkbox is checked and pass it to the controller in an Ajax call
            $.ajax({
                type: "POST",
                url: '@Url.Action("Action", "Controller")',

                data: "{parameter:parameter, status:'" + check + "'}",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                }
            });
        }

如果您不明白某些事情,请告诉我!

于 2012-09-28T11:22:57.050 回答