2009-02-04 36 views
2

我已经这个加载之前与相应的颜色变量中的单选按钮:如何将JSON URL传递给jQuery函数?

<script type="text/javascript"> 
    //<![CDATA[ 
    images = {"Black":"http:\/\/ecx.images-amazon.com\/images\/I\/31XwBA2m8pL.jpg", "Slate":"http:\/\/ecx.images-amazon.com\/images\/I\/31SqWkrSb6L.jpg", "White":"http:\/\/ecx.images-amazon.com\/images\/I\/31OJufgJT2L.jpg"}; 
    //]]> 
</script> 

这里是jQuery函数。目前为止我能做的最好的事情是让第一个图像消失,留下alt属性。

function change_large_product_image(url) { 
    jQuery('.largeprod img').attr('src', url); 
    return false; 
} 

以下是单选按钮的示例源。

<label for="Black" 
     style="background: #fff url(http://ecx.images-amazon.com/images/I/11TeKFAzJsL._SL30_.jpg) center center no-repeat;" 
     title="Black" > 
    Black 
</label> 
<input 
    id="color_black" 
    name="color" 
    onclick="change_large_product_image(images['Black'])" 
    type="radio" 
    value="Black" /> 
+0

这是什么做的?你想做什么? – 2009-02-04 21:09:09

回答

2

该参数的值是多少?url?看起来您可能需要解码URL字符串以删除'\'字符。

function change_large_product_image(url) { 
    jQuery('.largeprod img').attr('src', decodeURI(url)); 
    return false; 
} 

另外,考虑采取thaBadDawg的建议。

2

解决问题的更简单的方法不是尝试和更改src,而是清空容器并在其位置放置新的img。例如。

<div class"largeprod"><img src="...old..." /></div> 

而且使用jQuery清理出来,如果你想获得真正聪明有了它,你可以在“预加载”在非显示DIV的图像,然后克隆重新装入

$('.largeprod').empty().append('<img src="...new..."/>'); 

和把它放在你想要的地方。我使用一个系统,将图像(和大量内容)放入该隐藏div中,将其保存为id以及它进入数组的位置,然后当图像加载完成时,onload事件触发并将其移动到它的位置应该去。

1

这里是我的解决方案:

<script type="text/javascript"> 
    var myImages = { 
     "Black":"http:\/\/ecx.images-amazon.com\/images\/I\/31XwBA2m8pL.jpg" 
     ,"Slate":"http:\/\/ecx.images-amazon.com\/images\/I\/31SqWkrSb6L.jpg" 
     , "White":"http:\/\/ecx.images-amazon.com\/images\/I\/31OJufgJT2L.jpg" 
    }; 
    $(document).ready(function() { 
     for (var i in myImages) { 
      html = '<br />'; 
      html += '<label for="'+i+'" style="background: #fff url('+myImages[i]+') center center no-repeat;" title="'+i+'" >'+i+'</label>'; 
      html += '<input id="'+i+'" name="color" onclick="$(\'#largeImage\').attr(\'src\',\''+myImages[i]+'\');" type="radio" value="'+i+'" />'; 
      html += '<br />'; 
      $('body').append(html); 
     } 
     $('body').append("<img id='largeImage'>"); 
    }); 
</script>