如何在shopify上将javascript值分配给liquid?

问题描述 投票:0回答:2

我从 javascript 代码中获取了 html 单选按钮的值,我想将该值分配给 shopify Liquid 代码的变量,我可以这样做吗。

<span id="selected_variant_title">{{ selected_variant_title }}</span>


<script>
    jQuery(document).ready(function() {
      // Handle change event for radio buttons
      $('input[type="radio"]').change(function() {
        // Retrieve the selected value
        var proteinValue = $('input[name="meal_radio_input"]:checked').val();
        console.log("Test", $('input[name="meal_radio_input"]:checked').val());
        });
    });
    var selected_variant_title = '';

function handleRadioChange() {
  var radioButtons = document.getElementsByName('meal_radio_input');
  for (var i = 0; i < radioButtons.length; i++) {
    if (radioButtons[i].checked) {
      selected_variant_title = radioButtons[i].value;
      console.log('Selected value: ' + selected_variant_title);
      break;
    }
  }

  // Update the content of the selected_variant_title span element
  var selectedVariantTitleElement = document.getElementById('selected_variant_title');
  selectedVariantTitleElement.textContent = selected_variant_title;
}

我想在液体变量中分配 selected_variant_title ,如 {% 分配 Protein_option = selected_variant_title %}

shopify liquid
2个回答
1
投票

由于 Liquid 是一种渲染的辅助语言,你将无法做到这一点。 相反,您可以将 Liquid 值分配给 JS 变量。


-1
投票

你不能这样做,因为首先运行液态代码,然后运行 Javascript,所以在这种情况下你需要完全使用 javascript。

© www.soinside.com 2019 - 2024. All rights reserved.