简体   繁体   English

JQuery正在更改复选框值,但复选框未显示为已选中

[英]JQuery is changing checkbox value but the checkbox is not displaying as checked

I have a list with a checkbox and some text (like email's inbox). 我有一个带有复选框和一些文本的列表(如电子邮件的收件箱)。 I want to check/uncheck the checkbox when i click anywhere on the list item (row). 当我单击列表项(行)上的任意位置时,我想检查/取消选中该复选框。 I have used following code on click event of the row: 我在行的click事件上使用了以下代码:

$(this).find(".cbx input").attr('checked', true);

For first time, it show the checked checkbox. 这是第一次显示选中的复选框。 But when I click select it next time the value of checkbox checked="checked" updates in the html code (i checked through firebug) but the checkbox does not displayed as checked. 但是当我点击下一次选中它时,复选框的值=“已检查”html代码中的更新(我通过firebug检查),但复选框没有显示为已选中。

With .attr() you set the value of an attribute in the DOM tree. 使用.attr()可以在DOM树中设置属性的值。 Depending on the browser, browser version and jQuery version (especially 1.6), this does not always have the desired effect. 根据浏览器,浏览器版本和jQuery版本(尤其是1.6),这并不总是具有所需的效果。

With .prop() , you manipulate the property (in this case, 'checked state'). 使用.prop() ,您可以操作属性(在本例中为“已检查状态”)。 I strongly recommend using .prop() in your case, so the following will toggle correctly: 我强烈建议您使用.prop() ,以便正确切换:

$(this).find(".cbx input").prop('checked', true);

For further information, please refer to the documentation of .prop() . 有关详细信息,请参阅.prop()文档

Almost right, just use 'checked' instead of true: 几乎是正确的,只需使用'checked'而不是true:

$(this).find(".cbx input").attr('checked', 'checked');

Update: Alternatively, you can use this with newer JQuery versions: 更新:或者,您可以在较新的JQuery版本中使用它:

$(this).find(".cbx input").prop('checked', true);

Easy, the solution is to use $('xxxxxx') .prop ('checked', true ) or false Test OK in Jquery 1.11.x . 很简单,解决方案是在Jquery 1.11.x中使用$('xxxxxx')。 prop ('checked', true )或false Test OK。

Cheers 干杯

FULL DEMO CODE EXAMPLE 完整的演示代码示例

Examples: (Code JavaScript checkbox for a table) 示例:( 表格的代码JavaScript复选框)

    function checkswich() {
        if ($('#checkbox-select').is(':checked') == true) {
            selectTodo();
        } else {
            deSelectTodo();
        }
    }
    function selectTodo() {
        //$('#tabla-data input.ids:checkbox').attr('checked', 'checked');
        $('#tabla-data input.ids:checkbox').prop('checked', true);
    }
    function deSelectTodo() {
        //$('#tabla-data input.ids:checkbox').removeAttr('checked', 'checked');
        $('#tabla-data input.ids:checkbox').prop('checked', false);
    }
    function invetirSelectTodo() {
        $("#tabla-data input.ids:checkbox").each(function (index) {
            //$(this).attr('checked', !$(this).attr('checked'));
            $(this).prop('checked', !$(this).is(':checked'));
        });
    }

Examples: (Code HTML checkbox for a table) 示例:( 表格的代码HTML复选框)

           <table id="tabla-data" class="table table-striped table-bordered table-hover table-condensed">
                <thead>
                    <tr>
                        <th class="text-center" style="width: 5px"><span class="glyphicon glyphicon-check"></span></th>
                        <th>Engine</th>
                        <th><a href="#" class="">Browser</a></th>
                        <th class="td-actions text-center" style="width: 8%;">Acciones</th>
                    </tr>
                </thead>
                <tbody id="tabla-data" class="checkbox-data">
                    <tr>
                        <td class="text-center"><input type="checkbox" class="ids" name="[]id" value=""></td>
                        <td>#########</td>
                        <td>#######</td>
                        <td class="td-actions text-center">
                            <?= $this->element('table-data-bts',['id'=>1234,'action'=>['view'=>'view', 'edit'=>'edit', 'delete'=>'delete']]) ?>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center"><input type="checkbox" class="ids" name="[]id" value=""></td>
                        <td>#########</td>
                        <td>#######</td>
                        <td class="td-actions text-center">
                            <?= $this->element('table-data-bts',['id'=>1234,'action'=>['view'=>'view', 'edit'=>'edit', 'delete'=>'delete']]) ?>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center"><input type="checkbox" class="ids" name="[]id" value=""></td>
                        <td>#########</td>
                        <td>#######</td>
                        <td class="td-actions text-center">
                            <?= $this->element('table-data-bts',['id'=>1234,'action'=>['view'=>'view', 'edit'=>'edit', 'delete'=>'delete']]) ?>
                        </td>
                    </tr>
                </tbody>
            </table>

Example: (Code ComponentHTML use JavaScript) 示例:( 代码ComponentHTML使用JavaScript)

<form id="form-datos" action="/url/demo/blablaaa" method="post" enctype="multipart/form-data" style="visibility: hidden;" >
<input type="hidden" name="accion" id="accion">

<div class="btn-group">
<span class="btn btn-default btn-xs"><input type="checkbox" id="checkbox-select" onclick="checkswich()"></span>
<button type="button" class="btn btn-default btn-xs dropdown-toggle dropdown-toggle-check" data-toggle="dropdown" aria-haspopup="true"
        aria-expanded="false">
    <span class="caret"></span>
    <span class="sr-only">Options</span>
</button>
<ul class="dropdown-menu">
    <li><a href="#" onclick="accion()">Action Demo</a></li>
    <li role="separator" class="divider"></li>
    <li><a href="#" onclick="selectTodo()">Select All</a></li>
    <li><a href="#" onclick="deSelectTodo()">UnSelet All</a></li>
    <li><a href="#" onclick="invetirSelectTodo()">Inverse Select</a></li>
</ul>

useing **.prop()** it worked for me !

$(document).on("click",".first-table tr",function(e){
if($(this).find('input:checkbox:first').is(':checked'))
{
    $(this).find('input:checkbox:first').prop('checked',false);
}else
{
    $(this).find('input:checkbox:first').prop('checked', true);
}
});

nothing initially worked for me in chrome though it was working fine in mozilla. 虽然它在mozilla中运行良好,但最初没有任何东西在chrome中工作。 Later found out that custom CSS styling caused the checkmark to be not visible. 后来发现自定义CSS样式导致复选标记不可见。

This solution did not work for me (ie. Setting the checked state with prop). 这个解决方案对我不起作用(即用prop设置检查状态)。

$("#checkbox-reminder").prop("checked", true);

While the underlying state always updated correctly, the display would not properly repaint. 虽然基础状态始终正确更新,但显示将无法正确重绘。

This can be solved with. 这可以用。

$("#checkbox-reminder").prop("checked", true).checkboxradio();
$('#checkbox-reminder').checkboxradio('refresh');

See this thread for more info. 有关详细信息,请参阅此主题

 $("[id*='" + value + "'] input").prop('checked', false);

attr也有效(但仅限第一次)但是prop始终有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM