简体   繁体   English

创建一个复选框,然后一键自动勾选所有其他复选框,并显示一个按钮

[英]Create one tick box then automatically ticked all other tick boxes with one click and also shows a button

So, i am trying to create a tick box that have thic action:所以,我正在尝试创建一个具有 thic 动作的复选框:

  1. The chkAll box, once been ticked, all other check boxes will also ticked. chkAll 复选框,一旦被勾选,所有其他复选框也会被勾选。
  2. When this action happens, a button will appear.发生此操作时,将出现一个按钮。

Then, another action I am trying to achieve is, if two or more checkboxes (not included the chkAll box is ticked, the same button will appear too. This button is like a zip file button, so, only if more than one check box is ticked, it will appear (enabled to be used).然后,我试图实现的另一个动作是,如果两个或多个复选框(不包括 chkAll 框被勾选,同样的按钮也会出现。这个按钮就像一个 zip 文件按钮,所以,只有当多个复选框被打勾,就会出现(可以使用)。

Currently the error I am facing is, 1.the chkAll check box needs to double click only then, the button appear.目前我面临的错误是,1. chkAll 复选框只需要双击,按钮就会出现。

2.And when I unclicked the chkAll check box, the button does disappear, but the other chckboxes are still tciked, which they suppose to be unticked too. 2.当我取消单击 chkAll 复选框时,该按钮确实消失了,但其他 chckboxes 仍然被选中,他们认为它们也没有被选中。

3.And when more than 1 check boxes are ticked, the button does not appear. 3.当超过1个复选框被勾选时,按钮不会出现。

I am new to php, jquery, so will really appreciate any guidance from you.我是 php、jquery 的新手,非常感谢您的任何指导。 Thank you.谢谢你。

 function toggleTick_Change() { $('input[type=checkbox]').change(function() { if ($("#chkAll").prop("checked")) { $('input[type=checkbox]').prop('checked', $(this).prop("checked")); $("#conditional-part").show(); } else if ($("#chkT").prop("checked")) { if ($("#chkT").length > 1) { $("#conditional-part").show(); } else { $("#conditional-part").hide(); } } else { $("#conditional-part").hide(); } }); }
 #conditional-part { display: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <div class="content"> <div class="container"> <div id="page-container"> <div id="page-header"> <h3>Manage Deliveries</h3> </div> <div id="page-content"> <table align="center"> <tr id="conditional-part"> <td class="input-group " role="group" aria-label="..." style="width: 85px; margin-top: 2px;"> <input type="number" class="form-control input-xs" id="cpyCN{$rows.id}" name="cpyCN{$rows.id}" value="1" style="font-family: Raleway, Tahoma, Arial; font-size: 8pt; padding: 2px 2px 2px 10px;" placeholder="Copies" min="1" max="999" /> <span class="input-group-btn"> <button id="btnPrintCNTicked{$rows.id}" type="button" class="btn btn-primary btn-xs" style="width: 40px;" onclick="javascript:btnPrintCNTicked_Click({$rows.id},$('#cpyCN{$rows.id}').val())">CN</button> </span> </td> </tr> </table> <div class="table-responsive" style="margin-top: 10px;"> <table class="table table-hover table-bordered" style="font-size: 8pt;"> <thead> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" id="chkAll" style="width: 15px; height: 15px;" onchange="toggleTick_Change()" /> </th> <th style="width: 40px;"> # </th> <th style="width: 140px;"> DELIVERY NO / <br>DATE / TYPE </th> </tr> </thead> <tbody> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" id="chkT" style="width: 15px; height: 15px;" onchange="toggleTick_Change()" /> </th> <th style="width: 140px;"> abcd </th> </tr> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" id="chkT" style="width: 15px; height: 15px;" onchange="toggleTick_Change()" /> </th> <th style="width: 140px;"> 1234 </th> </tr> </tbody> </table> </div> <!--/id -page content--> </div> <!--/id -page container--> </div> <!-- /container --> </div> <!-- /content -->

Two things you should be aware of:您应该注意两件事:

  • #id should be used sparingly (preferably not at all), I have changed all #id to classes (with the exception of the ugly button and input). #id应该谨慎使用(最好根本不用),我已将所有#id更改为类(除了丑陋的按钮和输入)。 The frequent use of #id will hinder if not cripple your code in the future. #id的频繁使用将在未来阻碍您的代码,如果不是削弱您的代码。 This is especially true if you use jQuery.如果您使用 jQuery,则尤其如此。

  • onEvent attribute handlers are discouraged as well especially if you use jQuery.也不鼓励使用 onEvent 属性处理程序,尤其是在您使用 jQuery 时。

 <button onclick="doNOTUseThisEventHandler()">...</button>

 $(document).ready(function() { $(".chkAll").on('change', function(event) { if ($(this).is(":checked")) { $('.chkT').prop('checked', true); $(".conditional-part").show(); } else { $('.chkT').prop('checked', false); $(".conditional-part").hide(); } }); });
 .conditional-part { display: none; }
 <div class="content"> <div class="container"> <div class="page-container"> <div class="page-header"> <h3>Manage Deliveries</h3> </div> <div class="page-content"> <table align="center"> <tr class="conditional-part"> <td class="input-group " role="group" aria-label="..." style="width: 85px; margin-top: 2px;"> <input type="number" id="form-control input-xs" class="cpyCN{$rows.id}" name="cpyCN{$rows.id}" value="1" style="font-family: Raleway, Tahoma, Arial; font-size: 8pt; padding: 2px 2px 2px 10px;" placeholder="Copies" min="1" max="999" /> <span class="input-group-btn"> <button id="btnPrintCNTicked{$rows.id}" type="button" class="btn btn-primary btn-xs" style="width: 40px;" onclick="javascript:btnPrintCNTicked_Click({$rows.id},$('#cpyCN{$rows.id}').val())">CN</button> </span> </td> </tr> </table> <div class="table-responsive" style="margin-top: 10px;"> <table class="table table-hover table-bordered" style="font-size: 8pt;"> <thead> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" class="chkAll" style="width: 15px; height: 15px;"> </th> <th style="width: 40px;"> # </th> <th style="width: 140px;"> DELIVERY NO / <br>DATE / TYPE </th> </tr> </thead> <tbody> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" class="chkT" style="width: 15px; height: 15px;"> </th> <th style="width: 140px;"> abcd </th> </tr> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" class="chkT" style="width: 15px; height: 15px;"> </th> <th style="width: 140px;"> 1234 </th> </tr> </tbody> </table> </div> <:--/id -page content--> </div> <.--/id -page container--> </div> <.-- /container --> </div> <.-- /content --> <script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

 function toggleTick_Change(e) { if(e.checked){ $('.chkBox').prop('checked',true); $("#conditional-part").show(); }else{ $('.chkBox').prop('checked',false); $("#conditional-part").hide(); } } $('.chkBox').click(function() { if ($('.chkBox:checked').length == $('.chkBox').length) { $('#chkAll').prop('checked', true); $("#conditional-part").show(); } else { $('#chkAll').prop('checked', false); $("#conditional-part").hide(); } });
 #conditional-part { display: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <div class="content"> <div class="container"> <div id="page-container"> <div id="page-header"> <h3>Manage Deliveries</h3> </div> <div id="page-content"> <table align="center"> <tr id="conditional-part"> <td class="input-group " role="group" aria-label="..." style="width: 85px; margin-top: 2px;"> <input type="number" class="form-control input-xs" id="cpyCN{$rows.id}" name="cpyCN{$rows.id}" value="1" style="font-family: Raleway, Tahoma, Arial; font-size: 8pt; padding: 2px 2px 2px 10px;" placeholder="Copies" min="1" max="999" /> <span class="input-group-btn"> <button id="btnPrintCNTicked{$rows.id}" type="button" class="btn btn-primary btn-xs" style="width: 40px;" onclick="javascript:btnPrintCNTicked_Click({$rows.id},$('#cpyCN{$rows.id}').val())">CN</button> </span> </td> </tr> </table> <div class="table-responsive" style="margin-top: 10px;"> <table class="table table-hover table-bordered" style="font-size: 8pt;"> <thead> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" id="chkAll" style="width: 15px; height: 15px;" onchange="toggleTick_Change(this)" /> </th> <th style="width: 40px;"> # </th> <th style="width: 140px;"> DELIVERY NO / <br>DATE / TYPE </th> </tr> </thead> <tbody> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" id="chkT" class="chkBox" style="width: 15px; height: 15px;" /> </th> <th style="width: 140px;"> abcd </th> </tr> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" id="chkT" class="chkBox" style="width: 15px; height: 15px;" /> </th> <th style="width: 140px;"> 1234 </th> </tr> </tbody> </table> </div> <!--/id -page content--> </div> <!--/id -page container--> </div> <!-- /container --> </div> <!-- /content -->

So this is mostly due to you triggering the change twice.所以这主要是由于您触发了两次更改。 once in line when a user selects it, Then again when you run your on change function.当用户选择它时排队一次,然后当您运行您的 on change function 时再次排队。

Just add a class of .js-checkbox to your checkbox's and remove the onchange.只需将.js-checkbox的 class 添加到您的复选框并删除 onchange。

$('.js-checkbox').change(function() {
  if ($(this).attr('id') === 'chkAll') {
    if ($(this).prop('checked') === true) {
      $('.js-checkbox').prop('checked', true);
    } else {
      $('.js-checkbox').prop('checked', false);
    }
  }
  
  const checked = $('.js-checkbox:checked');
  
  if (checked.length >= 2) {
    $('#conditional-part').show();
  } else {
    $('#conditional-part').hide();
  }
})

the JS way... JS方式...

 const myForm = document.querySelector('#my-form'), chkbxN = document.querySelectorAll('#my-form input[type="checkbox"]'); myForm.onsubmit = e => e.preventDefault() // disable form submit myForm.oninput = e => { if (e.target.name==='All' && myForm.All.checked) chkbxN.forEach(cb => cb.checked = true) let chkCount = [...chkbxN].reduce((sum,cb)=>sum+(cb.checked?1:0),0) myForm.theButton.classList.toggle('noDisplay',(chkCount < 2)) }
 label { display: block; }.noDisplay { display: none; } #my-form > p { height: 2.2em; background: #a9bccf; padding: .3em; }
 <form id="my-form"> <p> <button name="theButton" type="button" class="noDisplay">button</button> </p> <label> <input type="checkbox" name="All" >All </label> <label> <input type="checkbox" name="car" >Car </label> <label> <input type="checkbox" name="house">House </label> <label> <input type="checkbox" name="nice" >nice </label> <label> <input type="checkbox" name="beach">Beach </label> </form>

I'll have to admit, my jQuery is pretty rusty.我不得不承认,我的 jQuery 已经很生锈了。 So, I did this with vanilla JS.所以,我用香草 JS 做到了这一点。

As was mentioned before, there are two checkboxes with the same id.如前所述,有两个具有相同 id 的复选框。 The id attribute needs to be unique for any element that uses one.对于任何使用 id 的元素,id 属性都必须是唯一的。 So I changed chkT to a class.所以我将chkT更改为 class。

I also changed the CSS property from display: none;我还从display: none;更改了 CSS 属性。 to visibility: hidden so that the table doesn't jump around so much when it is shown/hidden. to visibility: hidden以便表格在显示/隐藏时不会跳动太多。 I added a class called .show and the property visibility: visible!important and then just add/remove that class as the different conditions are met.我添加了一个名为 .show 的.show和属性visibility: visible!important ,然后在满足不同条件时添加/删除 class 。

I also removed the onchange attribute from the checkboxes and added the event listeners in the javascript.我还从复选框中删除了onchange属性,并在 javascript 中添加了事件侦听器。

Hopefully, this helps and didn't confuse you further.希望这会有所帮助,并且不会让您进一步困惑。 JQuery is an oaky tool, however, I would really recommend that you try and learn more of the vanilla Javascript methods to build a good JS foundation. JQuery 是一个橡木工具,但是,我真的建议您尝试并学习更多普通的 Javascript 方法来构建良好的 JS 基础。

 function toggleTick_Change(event) { const chkAll = document.querySelector(`#chkAll`); const chkT = document.querySelectorAll(`.chkT`); const condPart = document.querySelector(`#conditional-part`); if (event.target.id === `chkAll`) chkT.forEach(n => n.checked = event.target.checked); const ckTCheckedCount = document.querySelectorAll(`.chkT:checked`).length if (ckTCheckedCount === 0) { chkAll.checked = false; chkAll.indeterminate = false; } else if (chkT.length === ckTCheckedCount) { chkAll.checked = true; chkAll.indeterminate = false; } else { chkAll.checked = false; chkAll.indeterminate = true; } ckTCheckedCount > 1? condPart.classList.add(`show`): condPart.classList.remove(`show`); } document.querySelectorAll(`input[type=checkbox]`).forEach(node => node.addEventListener(`click`, event => toggleTick_Change(event)));
 #conditional-part { visibility: hidden; }.show { visibility: visible;important; }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="content"> <div class="container"> <div id="page-container"> <div id="page-header"> <h3>Manage Deliveries</h3> </div> <div id="page-content"> <table align="center"> <tr id="conditional-part"> <td class="input-group " role="group" aria-label="..." style="width: 85px; margin-top: 2px;"> <input type="number" class="form-control input-xs" id="cpyCN{$rows.id}" name="cpyCN{$rows.id}" value="1" style="font-family: Raleway, Tahoma, Arial; font-size: 8pt; padding: 2px 2px 2px 10px;" placeholder="Copies" min="1" max="999" /> <span class="input-group-btn"> <button id="btnPrintCNTicked{$rows.id}" type="button" class="btn btn-primary btn-xs" style="width: 40px;" onclick="javascript:btnPrintCNTicked_Click({$rows.id},$('#cpyCN{$rows.id}').val())">CN</button> </span> </td> </tr> </table> <div class="table-responsive" style="margin-top: 10px;"> <table class="table table-hover table-bordered" style="font-size: 8pt;"> <thead> <tr class="panel-default" style="height: 30px;"> <th style="width: 20px;"> <input type="checkbox" id="chkAll" style="width: 15px; height: 15px;" title="Select All" /> </th> <th style="width: 40px;"> # </th> <th style="width: 140px;"> DELIVERY NO / <br>DATE / TYPE </th> </tr> </thead> <tbody> <tr class="panel-default" style="height: 30px;"> <td style="width: 20px;"> <input type="checkbox" class="chkT" style="width: 15px; height: 15px;" /> </td> <td style="width: 140px;"> abcd </td> <td/> </tr> <tr class="panel-default" style="height: 30px;"> <td style="width: 20px;"> <input type="checkbox" class="chkT" style="width: 15px; height: 15px;" /> </td> <td style="width: 140px;"> 1234 </td> <td/> </tr> </tbody> </table> </div> <!--/id -page content--> </div> <!--/id -page container--> </div> <!-- /container --> </div> <!-- /content -->

暂无
暂无

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

相关问题 你怎么能告诉 reactJs 每个复选框都是唯一的,所以当我更改一个框的 checked 属性时,它不会勾选所有其他框? - How can you tell reactJs that each checkbox is unique so when I change the checked attribute for one box it doesn't tick all the other boxes? 遍历除最后一个刻度之外的所有刻度线 - Iterating over all tick marks except the last one 高库存:一次显示多个系列的所有二维条 - Highstock: show all two dimension bar of multi series on one tick 复选框打勾时,如何将在一个文本框中输入的数据显示到另一个文本框中? - How display data entered in one text box to another text box on checkbox tick? 从一个盒子创建多个盒子 - Create Multiple Boxes from One Box 如果没有选中一个框,如何禁用ALLOW按钮? 仅在选中所有复选框后才应启用ALLOW按钮吗? - How to disable ALLOW button if even one box in unchecked? ALLOW button should be enabled only if all the boxes are checked? 更改滚动距离的滚动距离 - Change scroll distance covered with one tick of the scrollwhell 当我单击复选框时,没有任何反应(提交) - Nothing happens when i click on the tick box (submit) 单击时显示勾号 - Show tick on click 我有2个文本框和一个按钮! 当我单击按钮时,它应该在所选文本框中写1,但是我的代码不起作用 - I have 2 text Boxes and one button! When I click on button it should write 1 in the chosen text box but my code is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM