我正在尝试根据人们在下拉菜单中选择的内容来切换图像。 html和js位于两个不同的文件中。
这只是 html 的一部分:
<select id="Breed_0_field" name="Breed76" onChange="javascript: wizard();" onkeyup="javascript: wizard();" tabindex="4">
<option selected="selected" value="Select One">Select One</option>
<option value="Other/Mix">Other/Mix</option>
<option value="0">--- Common Breeds----</option>
<option value="Border Collie">Border Collie</option>
<option value="Boxer">Boxer</option>
<option value="Corgi">Corgi</option>
<option value="Dachshund">Dachshund</option>
对于 JS 部分:
breed = document.getElementById("Breed_0_field").value;
selectdog()
function selectdog()
{
dog_type=0
switch (breed)
{
case "Affenpinscher": // Affenpinscher
// Affenpinscher
document.getElementById("dog_breed_picture").src = "http://handicappedpet.net/helppets/images/avatars/gallery/Dog_Larger/blank.gif";
break;
case "Affenpinscher": // Affenpinscher
// Affenpinscher
document.getElementById("dog_breed_picture").src = "http://handicappedpet.net/helppets/images/avatars/gallery/Dog_Larger/Affenpinscher.gif";
break;
case "Afghan Hound": // Afghan Hound
// Afghan Hound
document.getElementById("dog_breed_picture").src = "http://handicappedpet.net/helppets/images/avatars/gallery/Dog_Larger/Afghan_Hound.gif";
break;
问题是,这一切都适用于数字。 html 部分中的值曾经是 1,2,3,4...并且正确的图像与 1,2,3,4 的大小写匹配...但是我需要 html 的值是它的名称,否则稍后代码下面只显示数字而不是品种名称。
如何让这个图像切换有点工作,但使用字符串而不是数字?
最佳答案
我的错!显然这确实有效!
我的问题是我有一个函数 if (品种 > 0){selectdog()} 这永远不会起作用,因为我不再使用数字。
但是你确实可以使用带大小写的字符串。
关于javascript - 带图像切换的下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15005020/