【问题标题】:jQuery/Magento - Getting selected div ID and display custom valuejQuery/Magento - 获取选定的 div ID 并显示自定义值
【发布时间】:2013-04-28 21:11:12
【问题描述】:

我想要做的是使用 jQuery 来获取所选图像的 ID/CLASS,每个图像都有自己独特的类(即 image-1、image-2 等),并让 jQuery 显示一个自定义值,我告诉它根据他们的选择在另一个 div 中显示。

到目前为止,我有:

    <div class="amconf-images-container" id="amconf-images-135">
<img id="amconf-image-66" src="media/amconf/images/66.jpg" class="amconf-image amconf-image-selected">
<img id="amconf-image-216" src="media/amconf/images/216.jpg" class="amconf-image">
<img id="amconf-image-218" src="media/amconf/images/218.jpg" class="amconf-image">
</div>

并尝试使用 jQuery:

    jQuery(function(){
var div = jQuery('.selectedimage')[0];
    jQuery("img#amconf-image-66.amconf-image amconf-image-selected").bind("change keyup", function(event){
    div.innerHTML = this.value = "Black";
}); });

多次获得每个可能的选择。除非有更有效的方法来做到这一点而不与 magento 的原型和自己的脚本冲突?

谢谢!

【问题讨论】:

    标签: jquery magento bing


    【解决方案1】:

    变化:

    img#amconf-image-66.amconf-image amconf-image-selected
    

    收件人:

    img#amconf-image-66.amconf-image.amconf-image-selected
    

    【讨论】:

    • 似乎没有工作:/ 无论用户选择什么,类都会从 class="amconf-image" 更改为 class="amconf-image aconf-image-selected" 。有什么方法可以让 jquery 根据具有该类的图像提取图像?顺便说一句,谢谢您的回答。
    【解决方案2】:

    将以下内容放入页脚的附加 HTML 部分中引用的脚本文件中,并在生成它们的任何代码中为每个 img 标记设置 data-color:

    jQuery(document).ready(function() {
        jQuery('#amconf-img-container img').each(function() {
          color = jQuery(this).data('color');
          jQuery(this).on('change keyup', function (event) {
            if(jQuery(this).hasClass('selected')) {
                div.innerHTML = event.data.color;
            }}, {color: color});
        });
    

    如果我理解你的目的,无论如何......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      相关资源
      最近更新 更多