【问题标题】:jquery click event resets check property of checkboxjquery点击事件重置复选框的检查属性
【发布时间】:2013-09-27 09:13:43
【问题描述】:

我有一个 checkboxlist 控件,在此控件中,我不希望每个复选框在单击复选框时触发事件(手动或以编程方式)。 checkboxlist 生成的 Html 代码如下所示:

<div id="divleft">
    <table id="MainContent_CheckBoxList1">
        <tbody>
            <tr>
                <td><input id="MainContent_CheckBoxList1_0" name="ctl00$MainContent$CheckBoxList1$0" onclick="router(this);" value="1" type="checkbox"><label for="MainContent_CheckBoxList1_0">Option1</label></td>
            </tr>
            <tr>
                <td><input id="MainContent_CheckBoxList1_1" name="ctl00$MainContent$CheckBoxList1$1" onclick="router(this);" value="2" type="checkbox"><label for="MainContent_CheckBoxList1_1">Option2</label></td>
            </tr>
            <tr>
                <td><input id="MainContent_CheckBoxList1_2" name="ctl00$MainContent$CheckBoxList1$2" onclick="router(this);" value="3" type="checkbox"><label for="MainContent_CheckBoxList1_2">Option3</label></td>
            </tr>
            <tr>
                <td><input id="MainContent_CheckBoxList1_3" name="ctl00$MainContent$CheckBoxList1$3" onclick="router(this);" value="4" type="checkbox"><label for="MainContent_CheckBoxList1_3">Option4</label></td>
            </tr>
        </tbody>
    </table>
</div>

单击复选框时,我将隐藏或显示 div。 div 看起来像:

 <div id="divright">
    <div id="divoption1" style="display: none;">
        I am in option1 div
    </div>
    <div id="divoption2" style="display: none;">
        I am in option2 div
    </div>
    <div id="divoption3" style="display: none;">
        I am in option3 div
        </div>
    </div>
</div>

我有一个 jquery 代码,它可以完成显示/隐藏 div 的繁重工作。

$(document).ready(function () {
    RunOnce();
});

function uncheckAllCheckboxes(previouscheckedCheckboxValue, currentcheckedCheckboxValue) {
    if (previouscheckedCheckboxValue != null && previouscheckedCheckboxValue != currentcheckedCheckboxValue) {
        window.isRunOnce = 'false';
        $('[id$=divleft]').find('input:checkbox[value="' + previouscheckedCheckboxValue + '"]').prop('checked', false).click();
        //variable used to avoid infinite loop
        window.isRunOnce = null;
    }
    return currentcheckedCheckboxValue;
}

function router(control) {
    if (control.value == '1') {
        Option1Controller(control.value);
    }
    if (control.value == '2') {
        Option2Controller(control.value);
    }
    if (control.value == '3') {
        Option3Controller(control.value);
    }
}

function Option1Controller(currentCheckBoxValue) {
    if ($('[id$=divleft]').find('input:checkbox[value="' + currentCheckBoxValue + '"]').is(':checked') == true) {
        $('[id$=divoption1]').show();

        if (window.isRunOnce == null) {
            window.previouscheckBoxValue = uncheckAllCheckboxes(window.previouscheckBoxValue, currentCheckBoxValue);
        }

    }
    else {
        $('[id$=divoption1]').hide();
    }
}

function Option2Controller(currentCheckBoxValue) {
    if ($('[id$=divleft]').find('input:checkbox[value="' + currentCheckBoxValue + '"]').is(':checked') == true) {
        $('[id$=divoption2]').show();
        if (window.isRunOnce == null) {
            window.previouscheckBoxValue = uncheckAllCheckboxes(window.previouscheckBoxValue, currentCheckBoxValue);
        }
    }
    else {
        $('[id$=divoption2]').hide();
    }
}

function Option3Controller(currentCheckBoxValue) {
    if ($('[id$=divleft]').find('input:checkbox[value="' + currentCheckBoxValue + '"]').is(':checked') == true) {
        $('[id$=divoption3]').show();

        if (window.isRunOnce == null) {
            window.previouscheckBoxValue = uncheckAllCheckboxes(window.previouscheckBoxValue, currentCheckBoxValue);
        }
    }
    else {
            $('[id$=divoption3]').hide();
    }
}
function RunOnce() {
    Option1Controller('1');
    Option2Controller('2');
    Option3Controller('3');
}

问题在于函数 uncheckAllCheckboxes,在此函数中,我取消选中以前选中的复选框: 我试过了:

$('[id$=divleft]').find('input:checkbox[value="' + previouscheckedCheckboxValue + '"]').prop('checked', false); 

以上查询取消选中相应的复选框但不触发 onclick 事件?

$('[id$=divleft]').find('input:checkbox[value="' + previouscheckedCheckboxValue + '"]').click(); just after the above query.

它会触发click事件但也会撤消1,所以没用。

$('[id$=divleft]').find('input:checkbox[value="' + previouscheckedCheckboxValue + '"]').prop('checked', false).click();

这个查询似乎也没有任何作用

我的要求很简单:我需要以编程方式选中/取消选中由父 ID 和复选框控件的值标识的复选框。选中/取消选中后,控件也应该触发点击事件。

如有任何帮助,将不胜感激。

注意:我是 jquery 方面的新手,因此欢迎对我的代码进行任何改进。

【问题讨论】:

标签: javascript jquery asp.net


【解决方案1】:

您是否考虑过使用单选按钮而不是复选框?它们具有您尝试使用复选框实现的相同行为。我创建了一个fiddle

HTML

<label for="checkbox1">Div 1</label>
    <input type="checkbox" name="div" id="checkbox1" data-div-id="1">
<label for="checkbox2">Div 2</label>
    <input type="checkbox" name="div" id="checkbox2" data-div-id="2">
<label for="checkbox3">Div 3</label>
    <input type="checkbox" name="div" id="checkbox3" data-div-id="3">

<hr/>

<div id="div1">DIV1</div>
<div id="div2">DIV2</div>
<div id="div3">DIV3</div>

<br/>

<label for="radio1">Div 4</label>
    <input type="radio" name="div" id="radio1" data-div-id="4">
<label for="radio2">Div 5</label>
    <input type="radio" name="div" id="radio2" data-div-id="5">
<label for="radio3">Div 6</label>
    <input type="radio" name="div" id="radio3" data-div-id="6">

<hr/>        

<div id="div4">DIV4</div>
<div id="div5">DIV5</div>
<div id="div6">DIV6</div>

JS

$(document).ready(function() {

    var checkboxes = $('input:checkbox'),
        radios     = $('input:radio'),
        divs       = $('div');

    // hide all divs
    divs.hide();

    checkboxes.change(function( e ) {
        var e = e || window.event,
            target = e.target || e.srcElement;

        $('#div' + $(target).data('div-id')).toggle();
    });

    radios.change(function( e ) {
        var e = e || window.event,
            target = e.target || e.srcElement;

        divs.hide();
        $('#div' + $(target).data('div-id')).toggle();
    });
});

您可以看到两者之间的比较。并且尽可能不再使用内联 js 和 css。如果不是用于表格数据,请尽量避免使用表格,因为它们会导致性能问题。阅读this

【讨论】:

  • 1) 您对单选按钮是正确的,但由于客户的限制,我必须在这里强制使用复选框。 2)感谢有关表格的建议,但正如您所知,asp.net的复选框列表控件会自动生成表格。
【解决方案2】:

我认为你使用了太多代码。

检查一下:

$('input[type="checkbox"]').change(function () {
    var this_index = $(this).closest('tr').index(); //check the index of the tr of this input
    $("#divright > div").eq(this_index).show(); //show the div inside divright that has same index as this_index
});

还有这个demo。我删除了所有内联函数调用。我认为这是一种更简单的方法。

【讨论】:

  • 感谢您的及时响应,我认为内联调用您的意思是我在每个复选框中都有的 onclick 事件?我必须写这么多代码,因为:1.原始代码更复杂,条件更多,2.我试图在 jquery 的帮助下在 javascript 中实现 mvc
  • 嗨,塞尔吉奥,因为我对 jquery 不太熟悉,您能否确认一下:1) 您已用此代码替换了所有基础设施代码,这是页面中所有复选框的单个事件.我不明白其他两行。你能详细说明一下吗,或者一些指向这个的指针会有所帮助
  • @shankbond,我不在,刚刚更新了一些小评论。你理解那些线的想法吗?我认为该代码可以替换您的所有代码...但也许您还有更多未在此处发布的功能
  • 感谢您帮助我,现在我理解了这些行的含义:代码说,无论单击哪个复选框,都会获取其索引,在 divright 中显示具有相同索引的 div。是的,塞尔吉奥,当前的代码改变了复选框的行为,就好像它们是单选按钮一样,一次只选择一个。还有一些代码可以将复选框的行为更改为多个复选框选择,为了简单起见,我离开了这部分:)
  • 最后一个问题:你为什么要取 tr 的索引?没有任何复选框?
猜你喜欢
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
相关资源
最近更新 更多