简体   繁体   English

测试是否选中了表格中的复选框

[英]Test if checkboxes in a table are checked

I have this huge(1334 pages) table. 我有这个巨大的表(共1334页)。 My problem: how can I test all created checkboxes and determine if they're "checked"? 我的问题:我如何测试所有创建的复选框并确定它们是否被“选中”? May be should I input "checkbox" in another way? 可能我应该以其他方式输入“复选框”吗? This is part of code from .ftl file. 这是来自.ftl文件的代码的一部分。 It's problem for me to understand all coded. 我很难理解所有编码。

    [#list result.content as item]
    [#assign publicationType = (item.esbPublicationType)?default("не опубликован") /]
    <tr>
        <td>
            <p><input type="checkbox" name="checkbox"</p>
        </td>
        <td>
            ${(item.createdTime.time?string("dd.MM.yyyy HH:mm:ss"))?default("не известно")}
        </td>
        <td>
            ${(item.visit.branchOffice.name)?default("не известно")}
        </td>
        <td>
            <a href="#" class="[#if publicationType == "ERROR"]alert round label[/#if] publication-info" visit_id="${item.visit.id}">
            ${publicationType}
            </a>
        </td>
        <td class="text-center">
            [#--<a href="#" class="tiny round  visit-info" visit_id="${item.visit.id}">Просмотр визита</a>--]
            <a href="[@spring.url "/visit/publicate?id=" + item.visit.id/]"
               target="_blank" class="tiny round  publicate" visit_id="${item.id}">Опубликовать</a>
        </td>
    </tr>
[/#list]

[#list result.content as item]
    [#assign publicationType = (item.esbPublicationType)?default("не опубликован") /]
    <tr>
        <td>
            <p><input type="checkbox" name="checkbox"</p>
        </td>
        <td>
            ${(item.createdTime.time?string("dd.MM.yyyy HH:mm:ss"))?default("не известно")}
        </td>
        <td>
            ${(item.visit.branchOffice.name)?default("не известно")}
        </td>
        <td>
            <a href="#" class="[#if publicationType == "ERROR"]alert round label[/#if] publication-info" visit_id="${item.visit.id}">
            ${publicationType}
            </a>
        </td>
        <td class="text-center">
            [#--<a href="#" class="tiny round  visit-info" visit_id="${item.visit.id}">Просмотр визита</a>--]
            <a href="[@spring.url "/visit/publicate?id=" + item.visit.id/]"
               target="_blank" class="tiny round  publicate" visit_id="${item.id}">Опубликовать</a>
        </td>
    </tr>
[/#list]

I tried this but it doesnt work: 我试过了,但是没有用:

<script type="text/javascript">
var button = document.getElementById("button");
button.addEventListener("click", function (){
    var checkbox = document.getElementsByName("checkbox");
    for(i=0; i < checkbox.length; i++)
    {
        if(checkbox[i].checked){
        console.log("Work");
    }else{
        alert("Ни один из визитов не выбран");
    }
    }
}, false);

To check all checkboxes, use $(':checkbox').prop('checked', true); 要选中所有复选框,请使用$(':checkbox').prop('checked', true);

<p><input type="checkbox" /></p>
<p><input type="checkbox" /></p>
<p><input type="checkbox" /></p>
<p><input type="checkbox" /></p> // all unchecked

// on document ready (or whenever you need)
$(':checkbox').prop('checked', true);

// Chekboxes are now all checked

http://jsfiddle.net/daCrosby/7QLEj/ http://jsfiddle.net/daCrosby/7QLEj/

If you'd like to determine if a checkbox is checked, use $('selector').prop("checked") . 如果要确定是否选中了复选框,请使用$('selector').prop("checked")

<p><input type="checkbox" checked /></p>
<p><input type="checkbox" checked /></p>
<p><input type="checkbox" checked /></p>
<p><input type="checkbox" /></p>
<p><input type="checkbox" /></p>
<p><input type="checkbox" checked /></p>
<p><input type="checkbox" /></p>
<p><input type="checkbox" checked /></p>
<p><input type="checkbox" /></p>

// on document ready (or whenever you need)
$(':checkbox').each(function(){
    if ($(this).prop("checked"))
        $(this).parent().append("<span>This is checked</span>");
});

http://jsfiddle.net/daCrosby/7B6t7/ http://jsfiddle.net/daCrosby/7B6t7/

Try this in order to 'check' all checkbox inside the table : 尝试此操作以“选中”表内的所有复选框:

 $(function () {

    $("table [type=checkbox]").attr("checked", true);
 });

For testing whether it is checked or not : 为了测试是否被检查:

if ($("table [type=checkbox]").attr("checked")==true){
    alert("checked");   
}else{
   alert("not checked");       
}

I could find a small mistake in your code , please close the input tag properly 我在您的代码中发现一个小错误,请正确关闭输入标签

    <td>
        <p><input type="checkbox" name="checkbox"/></p>
    </td>

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

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