jQuery,在选择时更改 IMG SRC - 搜索图像
我不久前问了一个问题,意识到我犯了一些错误,我重新思考了我的做法,并认为我会再次提出一个干净的问题。
我有一个选择列表,
<select id="product-variants" name="id">
<option title="AFX4000ZC-KLYSM" value="54964022" id="variant-54964022">Kelly Green</option>
<option title="AFX4000ZC-GAR3X" value="55708562" id="variant-55708562">Garnet</option>
<option title="AFX4000ZC-CHTXL" value="55708752" id="variant-55708752">Gun metal Heather</option>
</select>
选择后我需要搜索此 UL
<ul style="display: none;" id="preload">
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-gunmetal-heather-icon_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-gunmetal-heather-icon-1_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-gunmetal-heather-icon-2_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-garnet-icon_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-garnet-icon-1_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-garnet-icon-2_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-kelly-green-icon_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-kelly-green-icon-1_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-kelly-green-icon-2_medium.jpg?1290984341">
</li>
</ul>
然后我需要它用 src 更新此图像
<div class="image">
<img src="default.jpg"/>
</div>
这是我目前所拥有的,但它不起作用,因为我的客户不会更改 的命名约定图像。谁能建议我解决这个问题的方法吗?
jQuery(function() {
jQuery('[name=id]').change(function() {
var sku = jQuery(this).find(':selected').attr('title');
// Looking for the preloaded image which src attribute contains the sku.
var new_src = jQuery('#preload img[src*=' + sku + ']').attr('src');
// Updating the main image and the href attribute of the anchor element that wraps it.
jQuery('div.image img').attr('src', new_src);
});
});
I asked a question a little while back and realised I'd made a few mistakes, I re-thought they way I'm doing it and thought I'd make a clean question again.
I have a select list,
<select id="product-variants" name="id">
<option title="AFX4000ZC-KLYSM" value="54964022" id="variant-54964022">Kelly Green</option>
<option title="AFX4000ZC-GAR3X" value="55708562" id="variant-55708562">Garnet</option>
<option title="AFX4000ZC-CHTXL" value="55708752" id="variant-55708752">Gun metal Heather</option>
</select>
Upon selection I need to search this UL
<ul style="display: none;" id="preload">
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-gunmetal-heather-icon_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-gunmetal-heather-icon-1_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-gunmetal-heather-icon-2_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-garnet-icon_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-garnet-icon-1_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-garnet-icon-2_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-kelly-green-icon_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-kelly-green-icon-1_medium.jpg?1290984341">
</li>
<li>
<img alt="" src="http://mysite.com/s/files/1/0055/1242/products/afx4000z-kelly-green-icon-2_medium.jpg?1290984341">
</li>
</ul>
Then I need it to update this image with the src
<div class="image">
<img src="default.jpg"/>
</div>
This is what I have at the moment but it won't work because my client will not change the naming convention of the images. Can anyone advise me of a way to get around this?
jQuery(function() {
jQuery('[name=id]').change(function() {
var sku = jQuery(this).find(':selected').attr('title');
// Looking for the preloaded image which src attribute contains the sku.
var new_src = jQuery('#preload img[src*=' + sku + ']').attr('src');
// Updating the main image and the href attribute of the anchor element that wraps it.
jQuery('div.image img').attr('src', new_src);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取每张图片的SRC,使用indexOf方法查看该SKU是否出现在图片src中,如果出现则将其更改为默认值。
Get the SRC of each image and use the indexOf method to see if the SKU appears in the image src, if it does, change it to default.