此功能正常。我需要jquery保持选定的值。在选择我有数据库的颜色主题。我需要从选定的值显示图像。对不起我的英语不好。
<label for=\"firma\"><span class=\"important\">*</span> Vyberte si farebnú tému:</label><br />
<select id=\"colour\" class=\"edt\" style=\"width:200px;\">
<option value=\"0\" data-src=\"http://icons.iconarchive.com/icons/hopstarter/button/16/Button-Delete-icon.png\">Zelená téma</option>
<option value=\"1\" data-src=\"http://icons.iconarchive.com/icons/gakuseisean/ivista-2/16/Misc-Delete-Database-icon.png\" selected>Modrá téma</option>
<option value=\"2\" data-src=\"img/redtshirt_thumb.png\">Červená téma</option>
</select>
</dl>
<dl>
<img id=\"imageToSwap\" src=\"http://icons.iconarchive.com/icons/hopstarter/button/16/Button-Delete-icon.png\">
</dl>
<script>
$(\"#colour\").on('change', function(){
$(\"#imageToSwap\").attr(\"src\", $(this).find(\":selected\").attr(\"data-src\"));
});
</script>
答案 0 :(得分:1)
我删除了&#34; \&#34;在Id,src等中,如下所示,它对我有用。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<label for="firma"><span class="important">*</span> Vyberte si farebnú tému:</label><br />
<select id="colour" class="edt" style="width:200px;">
<option value="0" data-src="http://icons.iconarchive.com/icons/hopstarter/button/16/Button-Delete-icon.png">Zelená téma</option>
<option value="1" data-src="http://icons.iconarchive.com/icons/gakuseisean/ivista-2/16/Misc-Delete-Database-icon.png" selected>Modrá téma</option>
<option value="2" data-src="img/redtshirt_thumb.png">Červená téma</option>
</select>
</dl>
<dl>
<img id="imageToSwap" src="http://icons.iconarchive.com/icons/hopstarter/button/16/Button-Delete-icon.png">
</dl>
<script>
$(document).ready(function(){
$("#colour").on('change', function(){
$("#imageToSwap").attr("src", $(this).find(":selected").attr("data-src"));
});
});
</script>
&#13;
答案 1 :(得分:0)
我不确定所有的反斜杠是什么。无论如何,我认为您正在尝试从data-src
属性中获取值。如果是这种情况,您希望这样做:
$("#imageToSwap").attr("src", $(this).find(":selected").data("src"));