【问题标题】:Jquery select option, using data attribute in Simplecart-jsJquery 选择选项,使用 Simplecart-js 中的数据属性
【发布时间】:2013-01-08 06:56:20
【问题描述】:

我正在尝试让 simplecart 为选择框中选择的每个尺寸图像设置价格。我为每个要出售的图像设置了选择框:

<img class="item_image" src='my.jpg' height='50' width='50' alt="Mine"/>
<span class="item_name">My image</span>

<select class="item_size" >
<option value="5 x 7" data-price="5.00">5 x 7 - <span>$ 5.00</span></option>
<option value="8 x 10" data-price="10.00">8 x 10 - <span >$ 10.00</span></option>
<option value="16 x 20" data-price="20.00">16 x 20 - <span>$ 20.00</span</option>
</select>
<input type="text" class="item_quantity"  title="Item Quantity" value="1"/>

当为每个项目选择选择选项值时,我正在尝试使用 data-price 属性设置价格:

 simpleCart.bind( 'beforeAdd' , function( item ){

$('select').each(function(){ 
  var selected = $(this).find('option:selected');
    var extra = selected.data('price'); 
     item.price(extra);
  });
  });

我是一个菜鸟,所以我不太会让它工作。我可以获得要设置的第一个图像价格,但不能在其他选择框中。我可以为每个选择框使用单独的 ID,但也不确定如何执行此操作。任何帮助将不胜感激!

【问题讨论】:

    标签: jquery simplecart


    【解决方案1】:

    您可以在更改选择框时更新的item_price field。

    <img class="item_image" src='my.jpg' height='50' width='50' alt="Mine"/>
    <span class="item_name">My image</span>
    
    <select class="item_size">
    <option value="5 x 7" data-price="5.00">5 x 7 - <span>$ 5.00</span></option>
    <option value="8 x 10" data-price="10.00">8 x 10 - <span >$ 10.00</span></option>
    <option value="16 x 20" data-price="20.00">16 x 20 - <span>$ 20.00</span</option>
    </select>
    <!-- new input --><input type="text" class="item_price" value="5.00"/>
    <input type="text" class="item_quantity"  title="Item Quantity" value="1"/>
    

    然后,您可以在 JS 中执行此操作

    $('.item_size').on('change', function(){
    
        var $this = $(this),
            selected = $this.find(':selected').data('price')
    
        $this.siblings('.item_price').val(selected);
    
    });
    

    http://jsfiddle.net/Kvkh5/ 上的工作示例

    不要忘记将新字段更改为“隐藏”字段,我将其设为文本字段仅用于演示目的。

    【讨论】:

      【解决方案2】:

      您可以这样做(在 Simplecart 网站上显示):

      simpleCart.bind( 'beforeAdd' , function( item )
      {
          if( item.get( 'size' ) == '15 x 10 cm' )
          {
              item.price( '2' );
          }
          else if( item.get( 'size' ) == '15 x 21 cm' )
          {
              item.price( '3' );
          }
          else if( item.get( 'size' ) == '21 x 29,7 cm' )
          {
              item.price( '5' );
          }
          else if( item.get( 'size' ) == '21 x 29,7 cm encadré' )
          {
              item.price( '20' );
          }
      });
      

      但它不适用于 ie 8。如果有人知道为什么?

      【讨论】:

      • 这个问题以及为什么我需要采取不同的做法是,并非每个 item.price() 对于每个尺寸都是相同的。例如,一件 15 x 10 厘米尺寸的商品的价格可能为 2,而另一件商品的价格可能为 4。需要为每个选项值设置数据价格。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多