需要双击复选框进行检查

时间:2017-05-31 06:27:36

标签: javascript c# jquery html razor

我有问题,我的代码不接受一个派系。有必要点击两次以打勾。 如果单击复选框一次,则不会显示勾号,但如果单击“保存”按钮,则在重新加载页面后,勾选此复选框。

JS

<script type="text/javascript">
var options = [];

$('.dropdown-menu a').on('click', function (event) {

    // This was your original function which sets the checkbox as checked or unchecked, and draws the check mark
    var $target = $(event.currentTarget),
        val = $target.attr('data-value'),
        $inp = $target.find('input[type="checkbox"]'),
        idx;

    if ((idx = options.indexOf(val)) > -1) {
        options.splice(idx, 1);
        setTimeout(function () { $inp.prop('checked', false) }, 0);
    } else {
        options.push(val);
        setTimeout(function () { $inp.prop('checked', true) }, 0);
    }

    $(event.target).blur();

    // This code is used to set the selection[] hidden input to true when the checkbox is checked 
    // and to false when the check mark is cleared
    $tap = $target.find('input[name*="selection"]');
    if ($inp.prop("checked")) {
        $tap.val('true');
    } else {
        $tap.val('false');
    }

    return false;
});

var length = $('.dropdown-menu > option').length;

var cb = $('.dropdown-menu input[type=checkbox]:checked'); // Selected chechboxes
var cnt = $('.CompNameBox'); // Container where to output text
if (cb.length === 1)
    cnt.text(cb.parent().text());
else if (cb.length > 1)
    cnt.text('Selected ' + cb.length + ' companies');
else if (cb.length === 0)
    cnt.text('Nothing selected');

HTML

 <div>
    <div class="row">
        <div class="col-md-1">
            <div class="btn-group" >
                <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" style="width: 180px"> <span class="CompNameBox"></span> <span class="caret"></span></button>
                <ul class="dropdown-menu" style="width: 180px">
                    @if (Model.AvailableCompanies != null)
                    {
                        for (int i = 0; i < Model.AvailableCompanies.Count; i++)
                        {
                            if (Model.Companies.Contains(Model.AvailableCompanies[i], new CompanyViewModelComparer()))
                            {
                            <li>
                                <a href="#" class="small" data-value="option1" tabIndex="-1">
                                    <input type="checkbox" checked />&nbsp;@Html.DisplayTextFor(model => Model.AvailableCompanies[i].NAME)
                                    <input type="hidden" name="selection[@i]" value="true"/>
                                    <input type="hidden" name="ids[@i]" value="@Model.AvailableCompanies[i].ID" />
                                </a>
                            </li>
                            }
                            else
                            {
                            <li>
                                <a href="#" class="small" data-value="option1" tabIndex="-1">
                                    <input type="checkbox" />&nbsp;@Html.DisplayTextFor(model => Model.AvailableCompanies[i].NAME)
                                    <input type="hidden" name="selection[@i]" value="false" />
                                    <input type="hidden" name="ids[@i]" value="@Model.AvailableCompanies[i].ID" />
                                </a>
                            </li>
                            }

                        }
                    }
                </ul>
            </div>
        </div>
    </div>
</div>

Chrome中的HTML

<div class="row">
        <div class="col-md-1">
            <div class="btn-group open">
                <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" style="width: 180px" aria-expanded="true"> <span class="CompNameBox">Selected 2 companies</span> <span class="caret"></span></button>
                <ul class="dropdown-menu" style="width: 180px">
                            <li>
                                <a href="#" class="small" data-value="option1" tabindex="-1">
                                    <input type="checkbox" checked="">&nbsp;TOro
                                    <input type="hidden" name="selection[0]" value="true">
                                    <input type="hidden" name="ids[0]" value="1">
                                </a>
                            </li>
                            <li>
                                <a href="#" class="small" data-value="option1" tabindex="-1">
                                    <input type="checkbox">&nbsp;My Comp
                                    <input type="hidden" name="selection[1]" value="false">
                                    <input type="hidden" name="ids[1]" value="2">
                                </a>
                            </li>
                            <li>
                                <a href="#" class="small" data-value="option1" tabindex="-1">
                                    <input type="checkbox" checked="">&nbsp;France
                                    <input type="hidden" name="selection[2]" value="true">
                                    <input type="hidden" name="ids[2]" value="3">
                                </a>
                            </li>
                            <li>
                                <a href="#" class="small" data-value="option1" tabindex="-1">
                                    <input type="checkbox">&nbsp;Germany Corp
                                    <input type="hidden" name="selection[3]" value="false">
                                    <input type="hidden" name="ids[3]" value="4">
                                </a>
                            </li>
                </ul>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

我认为问题发生在var options = []中,初始化时没有任何价值,但是您的html代码中的某些输入标签已被检查。因此,当您第一次单击时,js函数无法从选项中获取此标记。从收藏夹中删除。请修改选项集合的初始化方法。