I am making the modal for select the spac and quantity in online web shop: the following…
- User
I am making the modal for select the spac and quantity in online web shop: the following is the related javascript css and html: (document).ready(function () { const modal = ('#spec-modal-product-spec-modal'); const variantsContainer = modal.find('#spec-modal-variants-container'); const qtyInput = modal.find('#spec-modal-qty'); const qtyMinusBtn = modal.find('.spec-modal-qty-minus'); const qtyPlusBtn = modal.find('.spec-modal-qty-plus'); const modalCloseBtn = modal.find('.spec-modal-close'); const modalForm = modal.find('#spec-modal-product-spec-form'); const modalError = modal.find('.spec-modal-error');
let currentProductData = null; let activeAddToCartBtn = null; // store clicked button to restore UI
function buildVariantOptions(variants) { if (!Array.isArray(variants) || variants.length === 0) { variantsContainer.html(''); return; }
// Trim and split attr_name const attrNames = variants[0].attr_name ? variants[0].attr_name.split(',').map(s => s.trim()) : []; if (attrNames.length === 0) { variantsContainer.html(''); return; } let attrValuesByName = {}; attrNames.forEach(attr => attrValuesByName[attr] = new Set()); variants.forEach(variant => { if (!variant.variant_values) return; const vals = variant.variant_values.split(',').map(s => s.trim()); vals.forEach((val, i) => { if (attrNames[i]) attrValuesByName[attrNames[i]].add(val); }); }); if (Object.values(attrValuesByName).every(set => set.size === 0)) { variantsContainer.html(''); return; } let html = ''; attrNames.forEach((attrName, groupIndex) => { html += `<div class="spec-modal-variant-group" data-variant-attribute="${attrName}"> <label class="spec-modal-variant-label">${attrName}:</label> <div class="spec-modal-variant-options" role="radiogroup" aria-label="${attrName} options">`; Array.from(attrValuesByName[attrName]).forEach((val, optionIndex) => { const optionId = `spec-modal-variant-${attrName}-${optionIndex}`; html += ` <label class="spec-modal-variant-option" for="${optionId}" tabindex="0" role="radio" aria-checked="false" data-variant-attr="${attrName}" data-variant-val="${val}"> <input type="radio" name="spec-modal-variant-${attrName}" id="${optionId}" value="${val}"> ${val} </label>`; }); html += '</div></div>'; }); variantsContainer.html(html); function updateSelectedStyles() { variantsContainer.find('.spec-modal-variant-option').each(function () { var label = $(this); var input = label.find('input[type="radio"]'); if (input.prop('checked')) { label.addClass('selected').attr('aria-checked', 'true'); } else { label.removeClass('selected').attr('aria-checked', 'false'); } }); } updateSelectedStyles(); variantsContainer.find('.spec-modal-variant-option').on('click', function () { var input = $(this).find('input[type="radio"]'); input.prop('checked', true).trigger('change'); updateSelectedStyles(); updatePriceInfo(); }).on('keydown', function (e) { if (e.key === ' ' || e.key === 'Enter') { e.preventDefault(); $(this).trigger('click'); } }); variantsContainer.find('input[type="radio"]').on('change', function(){ updateSelectedStyles(); updatePriceInfo(); }); - Assistant