javascript - Why is my table being injected into the html multiple times with jQuery -
javascript - Why is my table being injected into the html multiple times with jQuery -
i'm using jquery dynamically homecoming list of products, render html , show on page, using $(selector).html(html)
.. i've noticed if add together products cart (which calls method render cart again, injecting html twice sometimes.
for example, below code renders menu:
html = " <table cellspacing=\"0\" width=\"270px\">\n"; html += " <tr>\n"; html += " <th>\n"; html += " <h1>\n"; html += " current order id: " + $("input[name=cart_id]").val() + "\n"; html += " </h1>\n"; html += " </th>\n"; html += " </tr>\n"; html += " <tr id=\"cart_products\">\n"; html += " </tr>\n"; homecoming $.ajax({ url: getajaxurl("cart/get_product"), data: args, type: 'get', global : false, success: function(data) { if ( data.success ) { //cart empty if ( data.data == "" ) { html += " <td>\n"; html += " cart empty!\n\n"; html += " </td>\n"; } else if ( ("data" in data) ) { html += " <td class=\"product\">\n\n"; html += " <table cellspacing=\"0\" width=\"100%\">\n\n"; html += " <tr>\n"; html += " <th>\n"; html += " product\n\n"; html += " </th>\n"; html += " <th>\n"; html += " qty\n\n"; html += " </th>\n"; html += " <th>\n"; html += " action\n\n"; html += " </th>\n"; html += " </tr>\n"; //loop on products in cart $.each(data.data, function(i, product){ if ( % 2 != 0 ) { row_class = " alt"; } else { row_class = ""; } if ( ! product.ok ) { row_class = " err"; } //check if error html += " <tr>\n"; html += " <td class=\"dsc" + row_class + "\">\n"; html += " <em>" + product.code + "</em><br />\n"; html += " " + product.description; if ( product.whqc != "" ) html += " <br />whqc: <strong>" + product.whqc + "</strong>\n"; else html += " \n"; html += " </td>\n"; html += " <td class=\"qty + " + row_class + "\">\n"; html += " <input id=\"qty_" + product.id + "\" name=\"qty\" type=\"text\" value=\"" + product.qty + "\" class=\"qty\" maxlength=\"5\" size=\"5\" />\n"; html += " </td>\n"; html += " <td align=\"center\" class=\"action" + row_class + "\">\n"; html += " <img style=\"cursor:pointer; cursor:hand\" data-product_id=\"" + product.id + "\" id=\"cart_product_update\" src=\"" + getimageurl() + "cart_product_update.png\" />\n"; html += " <img style=\"cursor:pointer; cursor:hand\" data-product_id=\"" + product.id + "\" id=\"cart_product_delete\" src=\"" + getimageurl() + "cart_product_delete.png\" />\n"; html += " </td>\n"; html += " </tr>\n"; }); html += " </table>\n"; html += " </td>\n"; } $("#cart_products").html(html); } else { alert("error"); } } });
starting @ line says html += "<td class=\"product\">\n\n";
pushed html twice.
any thought why , how stop it? thought using html
method safe doesn't append- replaces, seems method stepping on own toes. know boolean values isloading = true
, i'd avoid that.
you should initialize html variable properly:
var html = '';
javascript jquery html
Comments
Post a Comment