向表单元素添加多个复选框 [英] adding multiple checkboxes to a form element

查看:26
本文介绍了向表单元素添加多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设:一个页面在表单元素之外有一些动态生成的复选框.

Let's suppose: a page with some dynamically generated checkboxes outside of a form element.

在用户选中某些复选框后,我希望将所有这些复选框(选中或未选中)附加到表单元素中,以便当用户单击提交"按钮时,表单会考虑复选框,它们的 ID、名称、数据名称和它们的状态(选中或未选中).这可能吗?

After the user has checked some of the checkboxes, I would love to append all those checkboxes (either checked or unchecked) into the form element so that when the user click the "submit" button, the form takes into account the checkboxes, their ids, names, data-names and their status (checked or unchecked). Is that possible ?

我在这里尝试了一个 codpen:https://codepen.io/anthonysalamin/pen/ZxvZpP?editors=1010 但到目前为止没有成功.

I have tried a codpen here: https://codepen.io/anthonysalamin/pen/ZxvZpP?editors=1010 but was unsuccessful sofar.

jQuery 代码:

//insert all checkbox input elements into the form id="reservation"
  $("#reservation").submit(function(evt) {
  // should append all checkboxes to the form
  $("<input type='checkbox' />").append("#reservation");
});

这里是我的 codpen 的屏幕截图

推荐答案

.append() 需要文本 HTML 作为输入.您已经传递了 #reservationid 选择器.要在 submit 事件中有效地将复选框添加到 form,您可以这样做:

.append() expects textual HTML as input. You have passed the id selector of #reservation instead. To effectively add the checkboxes to the form in the submit event you could do this:

// wait for DOM to be ready
$( document ).ready(function() {
    //define the variable for all checkboxes on the page
    var allCheckboxes = $("input:checkbox[class=outsider]"); 
    //calls the append function on form submission
    $("#form-element").submit(function(event) {
        //insert all checkbox type elements into the form with id="form-element"
    var checkHTML = "";
    for(var checkboxIndex = 0; checkboxIndex < allCheckboxes.length; checkboxIndex++) {
      checkHTML += allCheckboxes[checkboxIndex].outerHTML;
    }
        $("#form-element").append(checkHTML);
    });
});

但是,您的意图很可能与行为不同.代码问题:

However, it is highly probable that your intention differs from the behavior. Problems with the code:

  • 您的复选框有 ID,如果我们克隆它们,那么您将拥有重复的 ID.由于如果有重复的 id 则 HTML 无效,因此克隆带有 id 的复选框将导致 HTML 无效
  • 选中状态不会被复制,因此您需要挖掘该值并将其放入新创建的复选框中

处理所有这些问题的代码:

Code dealing with all these problems:

// wait for DOM to be ready
$( document ).ready(function() { 
    //define the variable for all checkboxes on the page
    var allCheckboxes = $("input:checkbox[class=outsider]"); 
    //calls the append function on form submission
    $("#form-element").submit(function(event) {
        //insert all checkbox type elements into the form with id="form-element"
    var checkHTML = "";
    var checked = [];
    for(var checkboxIndex = 0; checkboxIndex < allCheckboxes.length; checkboxIndex++) {
      checked.push($(allCheckboxes[checkboxIndex]).prop('checked'));
      allCheckboxes[checkboxIndex].remove();
      checkHTML += allCheckboxes[checkboxIndex].outerHTML;
    }
        $("#form-element").append(checkHTML);
    allCheckboxes = $('input:checkbox[class=outsider]');
    console.log(checked);
    for (var checkboxIndex = 0; checkboxIndex < allCheckboxes.length; checkboxIndex++) {
      $(allCheckboxes[checkboxIndex]).prop('checked', checked[checkboxIndex]);
    }
    event.preventDefault();
    });
});

因此,如果您处理了所有问题,则可以通过克隆复选框来解决问题.或者,您可以使用 hidden input 在其中放置键值对,其中键将是 checkbox ids,值将是它们的相应的 checked 状态并将这些值放入 hidden inputsubmit 处理程序中.如果您不打算直观地将复选框放入表单中并且您对正确处理数据感到满意,那么将键值对的 JSON 值放入一个 hidden 的 value input 与将复选框复制到 form 相比要优越得多,但这只是细微差别.

So you can solve your problem by cloning the checkboxes if you handle all the problems. Alternatively, you could use a hidden input where you could put key-value pairs, where the keys will be checkbox ids and values will be their corresponding checked states and put those values into the hidden input at the submit handler. If you do not specifically intend to visually put the checkboxes into the form and you are satisfied with correct handling of data, then putting the JSON value of key-value pairs into the value of a hidden input is superior in comparison of copying checkboxes into the form, but these are only nuances.

这篇关于向表单元素添加多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆