简体   繁体   English

比较表单提交时复选框的选中状态与页面加载状态

[英]Comparing checkbox checked state versus page load state on form submit

I have multiple checkboxes on page and on form submission, I wish to not send all those checkboxes for processing, but only those whose state has changed from the one on page load. 我在页面和表单提交上有多个复选框,我希望不发送所有这些复选框进行处理,而只发送状态已从页面加载状态更改的那些复选框。

For example, on page load a checkbox's original state was checked. 例如,在页面加载时,选中了复选框的原始状态。

Then the user clicks on that same checkbox 3 times and then clicks the submit button: 然后,用户单击同一复选框3次,然后单击“提交”按钮:

  • 1st click - State changed to unchecked 第一次点击-状态更改为未选中
  • 2nd click - State changed to checked 第2次点击-状态更改为选中
  • 3rd click - State changed to unchecked 第三次点击-状态更改为未选中

So is there a way we can compare the 3rd click state with the page load state? 那么有没有办法将第三次点击状态与页面加载状态进行比较? In this case, I would want to send that checkbox data since its state has changed. 在这种情况下,由于状态已更改,我想发送该复选框数据。 On the other hand, if the user clicks two times, I do not want to send it for processing since its state has gone back to the same during page load. 另一方面,如果用户单击两次,我不想发送它进行处理,因为它的状态在页面加载期间已恢复为原来的状态。

When you submit a html form, all checkboxes are submitted unless they have "disabled" attribute. 提交html表单时,将提交所有复选框,除非它们具有“ disabled”属性。

One strategy may be adding an hidden field holding checkbox's value, and check these two against each other onSubmit, and disable the checkbox if they contain the same value. 一种策略可能是添加一个包含复选框值的隐藏字段,并在onSubmit上对这两个复选框进行检查,如果它们包含相同的值,则禁用该复选框。

In that case you can try some thing like this: 在这种情况下,您可以尝试以下操作:

$(document).ready(function(){
   // get check box state at the time of page load
   if($('#chk1').is(':checked'))
   {
       var chkState1 = true;
   }
   else
   {
       var chkState1 = false;
   }

   $('#chk1').change(function(){
       // get new state of this check box and compare it with the old one. If its state is diff than put it in the ajax call otherwise not
   });
});

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

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