【问题标题】:Add and remove hidden input for each checkbox that is checked为选中的每个复选框添加和删除隐藏输入
【发布时间】:2015-05-24 09:03:08
【问题描述】:

如何在选中复选框列表中的复选框时创建隐藏的输入元素,并在取消选中复选框时将其删除。见附件示例。

<div class="form-group">
<label class="h4" for="location">
    Industry
</label>

<div class="scrollable">
    <div class="checkbox margin-top-0">
        <label>
            <input type="checkbox" value="126">
            Arts, Music
        </label>
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox" value="127">
            Banking, Finance
        </label>
    </div>
</div>

<div>
<h5 class="margin-bottom-5">Selected</h5>
<ul>
    <li data-id="127">
        Banking, Finance
        <a >
            <i class="icon-after" aria-hidden="true" data-icon="x"></i>
        </a>
        <input type="hidden" value="127" name="industry" data-id="127">
    </li>
    <li data-id="126">
        Arts, Music
        <a >
            <i class="icon-after" aria-hidden="true" data-icon="x"></i>
        </a>
        <input type="hidden" value="126" name="industry" data-id="126">
    </li>
</ul>
</div>

【问题讨论】:

    标签: javascript jquery html css checkboxlist


    【解决方案1】:
     $('#checkbox1').change(function() {
        if($(this).is(":checked")) {
           $(this).after('<input type="hidden" >');
        }else{
        $(this).next('input["type=hidden"]').remove();              
        }   
      }             
    });
    

    在您的情况下,您需要在您创建的 ul 上调用或删除函数。

    【讨论】:

    • 欢迎来到 SO。只是回答问题的提示,请记住,您的答案不应该只是用户提出问题的参考。这是一个 wiki 站点,您的回答应该是访问此页面的每个人的参考。简述你的代码做什么,它是如何工作的,最重要的是,它应该在哪里使用。
    • 如何在隐藏字段中包含正确的检查值?您如何还可以在隐藏输入旁边使用标签来删除它?
    • 你可以使用 $(this).value() 检索值。
    • '' 使用这个设计,你可以使用 $(this).next('label' ).text() 检索标签内的文本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    相关资源
    最近更新 更多