选择框选择所有功能

Javascript Selectbox Select All Feature

本文关键字:选择 功能      更新时间:2023-09-26

我试图在我的按钮上添加选择所有功能,但有一个jquery的问题。最小的文件。

我在header。php页面上有这些

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>

我使用这些代码来选择所有函数;

   <script type="text/javascript">
function toggle(source) {
  checkboxes = document.getElementsByName('foo');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  }
}
</script>

当我在header.php上删除javascript包含并在ket.php上添加这些包含时,选择所有框的事情都在工作,但其他事情却没有(我们使用jquery做一些事情)。我需要使两者都工作。

这是我的html;

<table class="table table-bordered table-check">
                        <thead>
                            <tr>
                                <th><input type="checkbox" onClick="toggle(this)" class="styled"></th>
                                <th align="center">Soru</th>
                                <th align="center">Durum</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td><input type="checkbox" name="foo" class="styled" /></td>
                                <td align="center">Aylık kazancınız nedir?</td>
                                <td align="center">Aktif</td>
                            </tr>
                            <tr>
                                <td><input type="checkbox" name="foo" class="styled" /></td>
                               <td align="center">Cinsiyetiniz nedir?</td>
                               <td align="center">Pasif</td>
                            </tr>

                        </tbody>
                    </table>

谢谢。

你可以试试:

<table class="table table-bordered table-check">
  <thead>
    <tr>
      <th>
        <input type="checkbox" class="styled" id="allChk" />
      </th>
      <th align="center">Soru</th>
      <th align="center">Durum</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="checkbox" name="foo" class="styled" />
      </td>
      <td align="center">Aylık kazancınız nedir?</td>
      <td align="center">Aktif</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="foo" class="styled" />
      </td>
      <td align="center">Cinsiyetiniz nedir?</td>
      <td align="center">Pasif</td>
    </tr>
  </tbody>
</table>
脚本

$(function(){
    $("#allChk").on('change', function(){
        $("input[name=foo]:checkbox").prop('checked', $(this).prop('checked'));
    });
    $("input[name=foo]:checkbox").on('change', function(){
        $("#allChk").prop('checked', 0 === $("input[name=foo]:not(:checked)").length);
    });
})