2014-07-16 42 views
0

我有這個巨大的(1334頁)表格。我的問題:我如何測試所有創建的複選框並確定它們是否被「檢查」?也許應該以另一種方式輸入「複選框」? 這是來自.ftl文件的代碼的一部分。對我來說理解所有編碼是個問題。測試表格中的複選框是否被選中

[#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] 

我試過,但它不工作:

<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); 

+0

使用':checked'輸入複選框... – j809

+0

請詳細說明一下我_how可以檢查布爾值,所有創建的複選框「檢查」 _如果你要檢查所有的人,你可以用'$('? ('checked',true)' – Satpal

+0

你想*設置*所有複選框選中/取消選中,或者你想*測試*如果他們都被檢查? –

回答

0

要檢查所有的複選框,使用$(':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/

如果您想確定是否選中了複選框,請使用$('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/

+0

我的錯誤,我的英語是如此糟糕,但我需要測試這個布爾值,而不是設置。謝謝。 –

+0

沒問題@IlyaBeleychev - 查看我的更新。 – DACrosby

+0

這真是我需要的。萬分感謝! –

0

爲了試試這個來 '檢查' 的所有複選框在表內:

$(function() { 

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

爲了測試它是否被選中與否:

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

我可能在代碼中發現一個小錯誤,請正確關閉輸入標記

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

謝謝,現在關閉) –

相關問題