在将检查的元素从div克隆到另一个div时,我面临一个问题。我能够得到选中的元素,但并不是所有的元素都附加在后面。这是我的密码
<section class="BusinessSelection">
<p>
<input type="checkbox" class="checkedList">
<span class="BuIcon"></span>
<span>Business Unit 1</span>
</p>
<p>
<input type="checkbox" class="checkedList">
<span class="BuIcon"></span>
<span>Business Unit 2</span>
</p>
</section>
JQUERY
var checkbox=$(this).find('.checkedList:checked');
if(checkbox.attr('checked'))
{
var htmlcode=checkbox.parent().clone();
($('#dispbusiness').html(htmlcode));
}
});
选中的元素正在重写,只有最后选中的元素被显示。
预期的输出检查元素及其同级应该显示在另一个下面。
发布于 2013-09-17 12:28:32
小提琴
$(document).on('click', 'input:checkbox',function(){
if($(this).is(':checked'))
{
var htmlcode=$(this).parents('section').clone();
$('#dispbusiness').html(htmlcode);
}
});
<section class="BusinessSelection">
<p>
<input type="checkbox" class="checkedList">
<span class="BuIcon"></span>
<span>Business Unit 1</span>
</p>
<p>
<input type="checkbox" class="checkedList">
<span class="BuIcon"></span>
<span>Business Unit 2</span>
</p>
</section>
<div id="dispbusiness"></div>
发布于 2013-09-17 12:26:11
JQUERY
$(".checkedList").change(function(){
var container=$(this)closest('section');
var chkList=$(".checkedList",container).is(":checked");
var result="";
chkList.each(function(){
result += $(this).innerHTML();
});
$(".BusinessSelectionSelected").innerHTMl=result;
});
<section class="BusinessSelection">
<input type="checkbox" class="checkedList">pamm</input>
<input type="checkbox" class="checkedList">pamm1</input>
<span class="BuIcon"></span>
<span>Business Unit 1</span>
</section>
<section class="BusinessSelectionSelected">
</section>
发布于 2013-09-17 12:31:06
在以下情况下尝试此条件:
if(checkbox.is(':checked')) // this will condition return boolean true or false
{
//yourcode
}
或者你可以用
if(checkbox.attr('checked')=="checked") // this condition will return string 'checked' or 'undefined'
{
//yourcode
}
https://stackoverflow.com/questions/18849352
复制相似问题