没有使用jquery在mvc中检查复选框..? [英] Not getting checkbox checked in mvc using jquery..?

查看:55
本文介绍了没有使用jquery在mvc中检查复选框..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@using (Html.BeginForm(null, null, FormMethod.Post, new { Id = "form" }))
{
    @Html.DropDownList("Roles",newSelectList((IEnumerable<EmployeeManagementSystem.DAO.RoleMaster>)ViewData["Roles"], "RoleId", "RoleName"), "Select Role", new { @id = "ddlRole" })
    <table id="sample_editable_1">
        <tbody>
            @{
                var i = 0;
                foreach (var item in Model)
                {
                    <tr>
                        <td style="display: none">@Html.HiddenFor(modelItem => item.privilegeId, new { @id = "hdnPrivilegeId" + i })
                        </td>
                        <td>@Html.CheckBox("CheckBox",false, new { @id = "chkRolePrivilege" + i ,@name="PrivilegeCheckBox"})
                        </td>
                        <td>@Html.LabelFor(modelITem => item.privilegeName, item.privilegeName, new { @id = "privilegeName" + i, Value = item.privilegeName })
                       </td>
                    </tr>
                    i++;
                }
            }
        </tbody>
    </table>
}







我需要根据角色的权限选中复选框。但是使用jquery在下拉列表的更改事件中未选中/取消选中复选框。但是可以在$(document).ready(function()中检查复选框,但是当我们在更改事件中执行它或者其检查的属性不起作用时,所有其他属性(如禁用等)都正常工作。< br $>





I need to check the checkboxes according to the privileges of the role.But the checkboxes are not checked/unchecked inside the change event of dropdown using jquery. But the check boxes able to be checked inside the $(document).ready(function() but when we do it inside a change event or something its checked attribute is not working and all other attribute such as disabled etc are working fine.

$("#ddlRole").change(function () {
  var roleId = 0;
        var privilegeListName = "";
        var row = $(this).parent().parent();
        roleId = $('#ddlRole :selected').val();
        $.ajax({
            url: "/api/Privilege/GetPrivilegesByRoleId?RoleId=" + roleId,
            type: 'Get',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {

                for (var i = 0; i < 4; i++) {
                    for (var j = 0; j < 4; j++) {
                        if ($('#sample_editable_1 tr #privilegeName' + i).text() == data[j].privilegeName.trim()) {

                            $('#chkRolePrivilege' + i).attr('checked', true);
                            alert("true");
                        }
                        else {
                            alert("false");
                        }
                    }
                }
            }
          });
        });

推荐答案

(文件).ready(function()但是当我们在更改事件中执行此操作或其检查的内容时属性不起作用,所有其他属性,如禁用等工作正常。



(document).ready(function() but when we do it inside a change event or something its checked attribute is not working and all other attribute such as disabled etc are working fine.


( #ddlRole)。change(function(){
var roleId = 0;
var privilegeListName =;
var row =
("#ddlRole").change(function () { var roleId = 0; var privilegeListName = ""; var row =


(这个).parent()。parent();
roleId =
(this).parent().parent(); roleId =


这篇关于没有使用jquery在mvc中检查复选框..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆