JQuery removing items from the DOM still shows reporting

I have an address search system in which the user enters a zip code, if the zip code is checked, then an address list is returned and an address bar is selected, the list disappears, and then the address bar is further divided into some form of inputs.

The problem I encountered is when they went through the above process, then cleared the zip code form field, click the address search button and the address list will reappear.

An event, although the list and parent tr were removed from the DOM, does it still report that it is present as length 1?

My code is as follows:

JQuery

// when postcode validated display box
var $addressList = $("div#selectAddress > ul").length;

// if address list present show the address list
if ($addressList != 0) {
    $("div#selectAddress").closest("tr").removeClass("hide");
}
// address list hidden by default
// if coming back to modify details then display address inputs
var $customerAddress = $("form#detailsForm input[name*='customerAddress']");

var $addressInputs = $.cookies.get('cpqbAddressInputs');

if ($addressInputs) {
    if ($addressInputs == 'visible') {
        $($customerAddress).closest("tr").removeClass("hide");
    }
} else {
    $($customerAddress).closest("tr").addClass("hide");
}
// Need to change form action URL to call post code web service
$("input.findAddress").live('click', function(){

    var $postCode = encodeURI($("input#customerPostcode").val());

    if ($postCode != "") {
        var $formAction = "customerAction.do?searchAddress=searchAddress&custpc=" + $postCode;
        $("form#detailsForm").attr("action", $formAction);
        } else {
            alert($addressList);}

});
// darker highlight when li is clicked
    // split address string into corresponding inputs
    $("div#selectAddress ul li").live('click', function(){

    $(this).removeClass("addressHover");

    //$("li.addressClick").removeClass("addressClick");

    $(this).addClass("addressClick");

    var $splitAddress = $(this).text().split(",");

    $($customerAddress).each(function(){
        var $inputCount = $(this).index("form#detailsForm input[name*='customerAddress']"); 
        $(this).val($splitAddress[$inputCount]);
    });

    $($customerAddress).closest("tr").removeClass("hide");      

    $.cookies.set('cpqbAddressInputs', 'visible');

    $(this).closest("tr").fadeOut(250, function() { $(this).remove(); });

}); 
+3
source share
1

, , . , 5 DIV (: var divs = $('. Mydivs');), jQuery remove() DIV, : divs .eq(0).remove(), , divs .size() 5 . , remove() DOM. ... remove() : divs = $('. Mydivs'); , , :

// get all 5 divs
var d = $('.dv');

// remove the first div
d.eq(0).remove();

// you would expect 4 but no, it 5
alert(d.size());

// re-set the variable
d = $('.dv');

// now we get 4
alert(d.size());
+7

Source: https://habr.com/ru/post/1739444/


All Articles