添加表单标记时出现未定义的错误

时间:2016-07-11 05:42:17

标签: javascript html

我使用下面的代码来更改显示的图像。当我向html添加表单标签时,我在IE中只会出现错误,表示“涂料未定义”。当我改变一个下拉。我花了很多年的时间寻找并尝试弄清楚但没有运气。有任何想法吗? Tks

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript">

    function callAFunction(SelectBox)
    {
        var caption=[
            'Default Image Caption',
            'Caption1',
            'Caption2',
            'Caption3',
            'Caption4',
            'Caption5',
            'Caption6',
            'Caption7',
            'Caption8',
            'Caption9'],
        bp='images/',
        imgnum=8,
        thumb=document.getElementById('thumb'),
        description=document.getElementById('caption');
        thumb.src=bp+'picture'+paint.value+vinyl.value+'.jpg';
        description.innerHTML=caption[SelectBox.value];
    }

</script>
</head>
<body>


<form>
<img src="images/picture1.jpg" alt="" id="thumb">

<div id="caption">Caption1</div>

<label>Change the Paint: </label>
<select id="paint" name="paint" onChange="callAFunction(this); return false;">
    <option value="1">Image 1</option>
    <option value="2">Image 2</option>
    <option value="3">Image 3</option>
    <option value="4">Image 4</option>
    <option value="5">Image 5</option>
    <option value="6">Image 6</option>
    <option value="7">Image 7</option>
    <option value="8">Image 8</option>
    <option value="9">Image 9</option>
</select><br />

<label>Change the Vinyl: </label>
<select id="vinyl" name="vinyl" onChange="callAFunction(this); return false;">
    <option value="1">Image 1</option>
    <option value="2">Image 2</option>
    <option value="3">Image 3</option>
    <option value="4">Image 4</option>
    <option value="5">Image 5</option>
    <option value="6">Image 6</option>
    <option value="7">Image 7</option>
    <option value="8">Image 8</option>
    <option value="9">Image 9</option>
</select>

</form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

paint.value不存在。请改用document.getElementById("paint").value。与vinyl.value相同。

答案 1 :(得分:0)

尝试

paint[paint.selectedIndex].value