Jquery remove,add and find query
In the following code,after removing the checkboxes and adding it again.The alert always becomes true for "could not find checkboxes"
<div id="r_n">
<div name="r_t"><input type="checkbox" name="r_name" />Name</div>
<div name="r_t"><input type="checkbox" name="r_name开发者_如何学Python" />Address</div>
<div name="r_t"><input type="checkbox" name="r_name" />Value</div>
<div name="r_t"><input type="checkbox" name="r_name" />Total</div>
</div>
<script>
$("r_t").remove();
$("r_n").html('');
Now all the checkboxes are removed form the dom
$("r_n").append('<div name="r_t"><input type="checkbox" name="r_name" />Name</div>
<div name="r_t"><input type="checkbox" name="r_name" />Address</div>
<div name="r_t"><input type="checkbox" name="r_name" />Value</div>
<div name="r_t"><input type="checkbox" name="r_name" />Total</div>');
if($("r_n :checkbox").length > 0) {
{
alert("Could not find checkboxes")
}
else
{
alert("Found");
}
$("r_t").remove();
should be $("div[name=r_t]").remove();
$("r_n").html('');
should be $("#r_n").html('');
$("r_n").append
should be $("#r_n").append
and
$("r_n :checkbox").length
should be $("#r_n :checkbox").length
finally you have an extra {
after the if
..`
Does it work with those changes ?
UPDATE
Finally your logic is wrong..
you say if the length is > then 0 (means that it found at least 1 checkbox) then show "Could not find checkboxes", but it should really be if length is == 0 (length of 0 means not checkboxes found)
精彩评论