Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在html中将选中的复选框数据附加到列表顶部<;部门>;标签?_Javascript_Jquery_Html_Checkbox - Fatal编程技术网

Javascript 如何在html中将选中的复选框数据附加到列表顶部<;部门>;标签?

Javascript 如何在html中将选中的复选框数据附加到列表顶部<;部门>;标签?,javascript,jquery,html,checkbox,Javascript,Jquery,Html,Checkbox,我在表格格式中尝试了它的工作,但在中它不工作。是否可以使用标记将选中的复选框数据附加到列表顶部。请尝试此代码。这可能对你有帮助 HTML代码 <ol type="1" id="List"> <div class="ollist"> <div class="group-title"><span>group one title</span></div> <div class="items">

我在表格格式中尝试了它的工作,但在
中它不工作。是否可以使用
标记将选中的复选框数据附加到列表顶部。

请尝试此代码。这可能对你有帮助

HTML代码

 <ol type="1" id="List">
  <div class="ollist">
    <div class="group-title"><span>group one title</span></div>
    <div class="items">
      <li class="item">
        <input type="checkbox" id="Check-1" value="1" class="checkbox-custom" name="item_name[]" />
        <label for="Check-1" class="checkbox-custom-label">List 1</label>
      </li>
    </div>
    <div class="items">
      <li class="item">
        <input type="checkbox" id="Check-2" value="2" class="checkbox-custom" name="item_name[]" />
        <label for="Check-2" class="checkbox-custom-label">List 2</label>
      </li>
    </div>
    <div class="items">
      <li class="item">
        <input type="checkbox" id="Check-3" value="3" class="checkbox-custom" name="item_name[]" />
        <label for="Check-3" class="checkbox-custom-label">List 3</label>
      </li>
    </div>
    <div class="items">
      <li class="item">
        <input type="checkbox" id="Check-4" value="4" class="checkbox-custom" name="item_name[]" />
        <label for="Check-4" class="checkbox-custom-label">List 4</label>
      </li>
    </div>
    <div class="items">
      <li class="item">
        <input type="checkbox" id="Check-5" value="5" class="checkbox-custom" name="item_name[]" />
        <label for="Check-5" class="checkbox-custom-label">List 5</label>
      </li>
    </div>
  </div>
</ol>
jQuery(function($) {
  $('.ollistollist .group-title').each(function() {
    $(this).nextUntil('.group-title', '.items').data('group', this);
  });

  $('.ollist .items').has('input:checked').prependTo('.ollistollist');

  $('.ollist').on('change', 'input', function() {
    var $item = $(this).closest('.items');
    if (this.checked) {
      $item.insertBefore($('.ollist .group-title').first())
    } else {
      $item.appendTo($item.data('group'))
    }
  });
})

你可以把这个作为样品检查一下


测试1

测试2

$('input[type=“checkbox”]”)。单击(函数(){ 如果($(this).is(“:checked”)){ var html=“”+$(this.val()+”

”; $(“#divList”).prepend(html); } });
我们需要更多的上下文。任何代码示例,您尝试过的事情?如果您知道append,那么您也必须知道prepend。
<p>
<input type="checkbox" id="cb1" value="Test1" />
<label for="cb1">Test1</label>
</p>
<p>
<input type="checkbox" id="cb2" value="Test2" />
<label for="cb2">Test2</label>
</p>

<div id="divList">
</div>

$('input[type="checkbox"]').click(function(){
   if($(this).is(":checked")){
   var html="<p>"+$(this).val()+"</p>";
    $("#divList").prepend(html);
   }
});