Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript获取复选框的复选框属性_Javascript_Checkbox_Checked_Inline Editing - Fatal编程技术网

如何使用javascript获取复选框的复选框属性

如何使用javascript获取复选框的复选框属性,javascript,checkbox,checked,inline-editing,Javascript,Checkbox,Checked,Inline Editing,我正在学习关于内联编辑的教程 现在我遇到了一个问题,我正试图编辑复选框,但我并没有做到这一点 我只是用checkbox替换了文本类型的输入,但我想还有更多。所以我的问题是,如何读取复选框的checked属性 这是我的密码 <script type="text/javascript"> function restoreRow(oTable, nRow) { var aData = oTable.fnGetData(nRow); var jqTd

我正在学习关于内联编辑的教程

现在我遇到了一个问题,我正试图编辑复选框,但我并没有做到这一点

我只是用checkbox替换了文本类型的输入,但我想还有更多。所以我的问题是,如何读取复选框的checked属性

这是我的密码

<script type="text/javascript">

    function restoreRow(oTable, nRow) {
        var aData = oTable.fnGetData(nRow);
        var jqTds = $('>td', nRow);

        for (var i = 0, iLen = jqTds.length; i < iLen; i++) {
            oTable.fnUpdate(aData[i], nRow, i, false);
        }

        oTable.fnDraw();
    }

    function editRow(oTable, nRow) {
        var aData = oTable.fnGetData(nRow);
        var jqTds = $('>td', nRow);
        jqTds[0].innerHTML = '<input type="text" value="' + aData[0] + '">';
        jqTds[1].innerHTML = '<input type="text" value="' + aData[1] + '">';
        jqTds[2].innerHTML = '<input type="checkbox" value="' + aData[2] + '">';
        jqTds[3].innerHTML = '<a class="edit" href="">Save</a>';

    }

    function saveRow(oTable, nRow) {
        var jqInputs = $('input', nRow);
        oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
        oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);
        oTable.fnUpdate('<input type="checkbox" checked="' + jqInputs[2].value + '">', nRow, 2, false);
        oTable.fnUpdate('<a class="edit" href="">Edit</a>', nRow, 3, false);
        oTable.fnDraw();
    }

    $(document).ready(function () {
        var oTable = $('#tbl').dataTable();
        var nEditing = null;

        $('#new').click(function (e) {
            e.preventDefault();

            var aiNew = oTable.fnAddData(['', '', '',
            '<a class="edit" href="">Edit</a>', '<a class="delete" href="">Delete</a>']);
            var nRow = oTable.fnGetNodes(aiNew[0]);
            editRow(oTable, nRow);
            nEditing = nRow;
        });

        $('#tbl a.delete').live('click', function (e) {
            e.preventDefault();

            var nRow = $(this).parents('tr')[0];
            oTable.fnDeleteRow(nRow);
        });

        $('#tbl a.edit').live('click', function (e) {
            e.preventDefault();

            /* Get the row as a parent of the link that was clicked on */
            var nRow = $(this).parents('tr')[0];

            if (nEditing !== null && nEditing != nRow) {
                /* Currently editing - but not this row - restore the old before continuing to edit mode */
                restoreRow(oTable, nEditing);
                editRow(oTable, nRow);
                nEditing = nRow;
            }
            else if (nEditing == nRow && this.innerHTML == "Save") {
                /* Editing this row and want to save it */
                saveRow(oTable, nEditing);
                nEditing = null;
            }
            else {
                /* No edit in progress - let's start one */
                editRow(oTable, nRow);
                nEditing = nRow;
            }
        });
    });
这是我的桌子

<p>
                    <a id="new" href="">Add new row</a></p>
                <table cellpadding="0" cellspacing="0" border="0" class="tbl" id="tbl">
                    <thead>
                        <tr>
                            <th>
                                Name
                            </th>
                            <th>
                                Description
                            </th>
                            <th>
                                Sample
                            </th>
                            <th>
                            </th>
                            <th>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        @if (Model != null)
                        {
                            foreach (var item in Model.TestFields)
                            { 
                            <tr>
                                <td>
                                    @Truncate(item.Name, 20)
                                </td>
                                <td>
                                    @Truncate(item.Description, 20)
                                </td>
                                <td>
                                    @Html.CheckBoxFor(i => item.Sample)
                                </td>
                                <td>
                                    <a class="edit" href="">Edit</a>
                                </td>
                                <td>
                                    <a class="delete" href="">Delete</a>
                                </td>
                            </tr>
                            }
                        }
                    </tbody>
                </table>
假设$symbol是jQuery,则可以执行以下操作:

$('#my_checkbox').is(':checked') // true if my_checkbox is checked, false otherwise.

这将解决许多选中复选框的问题

// #BoxWrapper is supposed to be the div that contains the checkbox this could be replaced            
// by $(document) if you are not using a div as a container. 
$("#BoxWrapper").find('input:checkbox').each(function(){
var checked = $(this).attr('checked');
if(checked){
 //here you do what needs to be done for checked ones
}
});

如果checkboxElement.checked{…}但我没有一个复选框,我可以有100个或更多。我通过代码jqTds[2]访问复选框;,作为aData[2]的一个复选框。基于您的解决方案,我提出了这一点,这正是我所需要的。var$checkbox=$nRow.find'input:checkbox';jqTds[2].innerHTML=;我很高兴解决了您的问题,非常欢迎您。并声明:var jqTds=$'>td',nRow;在saveRow函数中。