需要禁用一组复选框

时间:2014-02-13 12:54:35

标签: javascript jquery asp.net-mvc-3 backbone.js

        <div class="result-content">
    <div class="table-container">
        <table class="table-names">
            <thead>
                <tr>
                    <th></th>
                    <th>Company Name
                    </th>
                    <th>Type
                    </th>
                    <th>Location
                    </th>
                    <th></th>
                    <th>Ultimate Parent
                    </th>

                </tr>
            </thead>
            <tbody>
                @{
                    int count = -1;

                }
                @foreach (Company company in Model)
                {
                    <tr>
                        @{
                            count = Model.IndexOf(company);
                            //var rParent = new System.Text.RegularExpressions.Regex(",| |&");
                            var contentParent =                   company.CompanyId.ToString().RemoveInvalidSymbolsInId();//rParent.Replace(company.CompanyId.ToString(), "_");
                            string value = string.Format("{0}|{1}", company.CompanyName, company.CountryRef.Name);
                            var comp = value.Split('|');

                            var parentCompanyId = company.UltimateCompanyId.ToString().RemoveInvalidSymbolsInId();
                            string ultimateParentName = string.Format("{0}", company.UltimateParentName);
                            var companyLevel = 0;

                        }
                        <td>
                            <div id="childSubsCheckBoxes">
                                <input type="checkbox" id="@string.Format("idRadio_{0}", contentParent)" 
                                       name="@ViewBag.UiElementMapper.SearchString"
                                       value="@ViewBag.UiElementMapper.SearchString"
                                       data-Criteria="@company.CompanyId"
                                       data-Operation="@ViewBag.UiElementMapper.Operation"
                                       data-Groupkey="@ViewBag.UiElementMapper.GroupKey"
                                       data-SearchString="@ViewBag.UiElementMapper.SearchString"
                                       data-Value="@string.Format("{0}", comp[0])"
                                       data-ValueType="@ViewBag.UiElementMapper.ValueType" />
                            </div>
                        </td>

                        <td>@company.CompanyName
                        </td>
                        <td style="padding-left: 10px;">@if (@company.CompanyLevel != null)
                                                        {
                                                            if (@company.CompanyLevel == 0)
                                                            {
                                                                <img src="@Url.Content("~/Content/images/ICTSP-Logo-red.gif")" title="" alt="Ultimate Parent" />
                                                            }

                                                            else
                                                            {
                                                                <img src="@Url.Content("~/Content/images/ICTSP-Logo-blue.gif")" title="" alt="Child/Subsidiary"/>
                                                            }
                                                        }
                        </td>
                        <td>@company.CountryRef.Name
                        </td>
                        <td>
                            @{
                                    <div id="parentCheckBoxes">
                                        <input type="checkbox" id="@string.Format("idRadio_{0}", parentCompanyId)" 
                                               name="@ViewBag.UiElementMapper.SearchString)"
                                               value="@ViewBag.UiElementMapper.SearchString"
                                               data-Criteria="@company.UltimateCompanyId"
                                               data-Operation="@ViewBag.UiElementMapper.Operation"
                                               data-Groupkey="@ViewBag.UiElementMapper.GroupKey"
                                               data-SearchString="@ViewBag.UiElementMapper.SearchString"
                                               data-ValueType="@ViewBag.UiElementMapper.ValueType" 
                                               data-Value="@string.Format("{0}", ultimateParentName)"
                                               data-Level="0"
                                            />
                                    </div>

                            }
                        </td>
                        <td>@if (@company.UltimateParentName != null)
                            {
                                <font title="@company.UltimateParentName">@Html.ActionLink(company.UltimateParentName, "GetCompanyInformation", new { companyId = company.UltimateCompanyId, companyName = company.UltimateParentName, guid = Guid.NewGuid() })</font>
                            }
                        </td>


                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

在上面的html ..我必须在div-parentCheckBoxes下单击复选框时禁用div-childSubsCheckBoxes下的复选框。我们已经有了一个单击此页面中复选框的功能。我编写了下面提到的代码,它只禁用第一个复选框而不是其他复选框。请告诉我哪里出错了。

clb.el.criteriaList.find('#childSubsCheckBoxes').each(function () {
            var checkbox =        clb.el.criteriaList.find('#childSubsCheckBoxes').find('input[type=checkbox]');
            checkbox.attr('disabled', true);

        });

点击我有此功能的任何复选框

   addCheckboxCriteria: function () {
        clb.el.criteriaList.find('#childSubsCheckBoxes').each(function () {
            var checkbox = clb.el.criteriaList.find('#childSubsCheckBoxes').find('input[type=checkbox]');
            checkbox.attr('disabled', true);
        });

        var t = $(this);
        var empty = clb.el.criteriaForm.find('div.criteria-item-checked').length;

        var searchString = $.trim(clb.el.criteriaMenuList.find('li.active').text());
        var currentTab = t.parents('.search-criteria-list').attr('id');
        var element;
        var tabString;
        var count = 0;
        clb.el.criteriaForm.find('.no-criteria').find('span').remove();
        if (currentTab) {
            var currentTabs = currentTab.replace(/ul-/, '');
        }
        count = clb.el.criteriaForm.find('div.criteria-item-checked').length;

        if ($('#' + currentTabs).parent().css('display') === 'block') {

            var tabString = $('#' + currentTabs).parent().find('li.active').find('a').attr('data-name');
            var idTab = $('#' + currentTabs).parent().find('li.active').attr('id');
        }
        if (!t.attr('checked') == false && count < clb.maxCriteriaCount ) {
            var divElement = $('<div/>', {
                'class': 'criteria-item-checked',
                id: 'div_' + t.attr('id'),
                'Name': t.attr('name'),
                'data-Start': t.attr("data-start"),
                'data-Criteria': t.attr("data-Criteria"),
                'data-Item': t.attr('data-Item'),
                'data-Operation': t.attr('data-Operation'),
                'data-Groupkey': t.attr('data-Groupkey'),
                'data-SearchString': t.attr('data-SearchString'),
                'data-Value': t.attr('data-Value'),
                'data-ValueType': t.attr('data-ValueType'),
                'data-Level':t.attr('data-Level')

            });
            var delElement = $('<a/>', {
                text: 'X',
                href: '#',
                'class': 'del-criteria'
            });
            if ($('#' + currentTabs).parent().css('display') === 'block') {
                var aElement = $('<a/>', {
                    text: tabString,
                    'class': 'a-criteria'
                });
            } else {
                var aElement = $('<a/>', {
                    text: searchString,
                    'class': 'a-criteria'
                });
            }
            var spanElement = $('<span/>', {
                text: ' ' + t.attr('data-Value')
            });

            delElement.appendTo(divElement);
            spanElement.appendTo(divElement);

            // remove criteria if tab is not empty
            if ($('#' + currentTabs).parent().css('display') === 'block') {
                clb.el.criteriaForm.find('ul').find('.data-tab').each(function () {
                    var li = $(this);
                    if (li.attr('data-name') === searchString && li.attr('data-group') !== idTab) {
                        li.remove();
                    }
                });
                t.parents('.partial-box').find('ul.search-criteria-list').not('#ul-' + currentTabs).find('input[type="checkbox"]').attr('checked', false);
            }

            if (clb.el.criteriaForm.find('ul').find('li').length) {
                clb.el.criteriaForm.find('ul').find('li').each(function () {
                    var t = $(this);
                    if (t.attr('data-name') === searchString) {
                        element = t;
                    }
                    if (clb.el.searchName) {
                        t.attr('data-companyname', clb.el.searchName.val());
                    }
                });
                if (element) {
                    if (element.find('div').length) {
                        var orElement = $('<font/>', {
                            text: ' OR'
                        });
                        orElement.appendTo(spanElement);
                    }
                    //divElement.appendTo(element);
                    element.find('a.a-criteria').after(divElement);
                } else {
                    var liElement = $('<li/>', {
                        'data-name': searchString
                    });
                    if ($('#' + currentTabs).parent().css('display') === 'block') {
                        liElement.addClass('data-tab');
                        liElement.attr('data-group', idTab);
                    }
                    if (clb.el.searchName) {
                        liElement.attr('data-companyname', clb.el.searchName.val());
                    }
                    aElement.appendTo(liElement);
                    divElement.appendTo(liElement);
                    liElement.appendTo(clb.el.criteriaForm.find('ul'));
                }
            } else {
                var liElement = $('<li/>', {
                    'data-name': searchString
                });
                if (clb.el.searchName) {
                    liElement.attr('data-companyname', clb.el.searchName.val());
                }
                if ($('#' + currentTabs).parent().css('display') === 'block') {
                    liElement.addClass('data-tab');
                    liElement.attr('data-group', idTab);
                }
                aElement.appendTo(liElement);
                divElement.appendTo(liElement);
                liElement.appendTo(clb.el.criteriaForm.find('ul'));
            }
            t.attr('checked', true);
            //clb.el.criteriaForm.parent().find('.btn').show();
            //clb.el.criteriaForm.find('.title').show();
            clb.el.criteriaForm.find('.no-criteria').hide();
        } else {
            var li = clb.el.criteriaForm.find('#div_' + t.attr('id')).parents('li:first');
            clb.el.criteriaForm.find('#div_' + t.attr('id')).remove();
            t.attr('checked', false);
            if (li.find('div.criteria-item-checked').length === 0) {
                li.remove();
            }
            if (clb.el.criteriaForm.find('li').length === 0) {
                //clb.el.criteriaForm.parent().find('.btn').hide();
                clb.el.criteriaForm.find('.no-criteria').show();
            }
            clb.checkLastDiv();
        }
    },

3 个答案:

答案 0 :(得分:0)

尝试:

   $('.sel').each(function()
   {
      $(this).prop('disabled','disabled');
   });

再次查看您的代码后,这应该有效:

$("input[type=checkbox]").attr("disabled", true);

答案 1 :(得分:0)

您可以使用jquery attr方法禁用复选框 如

           $("input[type=checkbox]").attr("disabled", true);

答案 2 :(得分:0)

我可以通过更改以下代码来解决它。问题是我提到了div id = childSubsCheckBoxes,而dom无法识别所有复选框。

我通过将div id属性更改为class来解决它..这是div class = childSubsCheckBoxes并使用下面的代码。它就像魔法一样。

clb.el.criteriaList.find('.childSubsCheckBoxes').each(function () {
            var checkbox =      clb.el.criteriaList.find('.childSubsCheckBoxes').find('input[type=checkbox]');
            checkbox.prop('disabled',true);
        });