最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Converting a dropdown to radio buttons - event not triggering - Stack Overflow

programmeradmin0浏览0评论

I've been able to get the dropdown values to change when the radio buttons are selected but the product image doesn't update like it's supposed to. Image only updates when the actual dropdown is used.

Here's the page:

And here's my code so far:

<script>

$(".variant-option select").each(function() {
  var select = $(this);
  var selectName = select.attr("name");

  // Create a container for the radio buttons
  var radioContainer = $("<div class='radio-container'></div>");

  select.find("option").each(function(i, e) {
    var optionValue = $(this).val();
    var optionText = $(this).text();

    // Create a radio button for each option
    $("<input type='radio' name='" + selectName + "' />")
    .attr("value", optionValue)
    .attr("id", selectName + "_" + i) // Add an ID for the label
    .attr("checked", i == 0)
    .click(function() {
        // Update the select element's value
        select.val(optionValue);

        // Trigger the change event on the select element
        select.trigger('change'); 
      })
    .appendTo(radioContainer);

    // Create a label for the radio button
    $("<label for='" + selectName + "_" + i + "'>" + optionText + "</label>")
    .appendTo(radioContainer);
  });

  // Add the radio buttons after the select element
  radioContainer.insertAfter(select);
});

</script>

Tried triggering the change event.

发布评论

评论列表(0)

  1. 暂无评论