设置无法获取YUI元素的默认SquareSpace产品变体

时间:2020-08-13 15:32:18

标签: javascript html yui squarespace

我正在尝试在Squarespace网站上为产品设置默认选项。

我正在使用盐水模板。

这是网站: https://pear-nectarine-6mdf.squarespace.com/childrens-subscription

密码是:qpbooks

我将以下代码注入页面:

<script>

function setValue() 
{

    var dropdown = document.querySelectorAll('[data-variant-option-name="Back Binding"]')[0];
    dropdown.value = 'Mixed';
    Y.one('#'+dropdown.id).simulate('change');

  
    var dropdown2 = document.querySelectorAll('[data-variant-option-name="Subscription Length"]')[0];
    dropdown2.value = '12 Months';
    Y.one('#'+dropdown2.id).simulate('change');
  
}

window.onload = setValue;

</script>

查看控制台时出现以下错误:

38: Y.one('#'+dropdown.id).simulate('change');

childrens-subscription:38 Uncaught TypeError: Cannot read property 'simulate' 
of null at setValue (childrens-subscription:38)

我认为这意味着YUI可能找不到我的选择框?

当我删除模拟调用时,代码会正确运行,并且选择框中的文本会更改,但产品价格不会更改。

我在访问YUI库文档时遇到问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您所引用的select元素没有id属性,因此,当您运行Y.one('#'+dropdown.id).simulate('change');时,将得到一个错误。 id上没有dropdown

相反,只需像这样将dropdown传递到Y.one(...)

Y.one(dropdown).simulate('change');

应该这样做。