我的网站设置如下:
<h1 class="testTitle"> Letter Height</h1>
<h2 class="testIns">Press the buttons to make it shorter or taller!</h2>
<div id="testBox">
<img src="images/height/heightMid.gif">
</div>
<form method="get" action="width.php" class="testAns" id="heightAns">
<input type="radio" name="height" value="short" onchange="ChangeDisplay();">-25%
<input type="radio" name="height" value="mid" onchange="ChangeDisplay();">normal
<input type="radio" name="height" value="tall" onchange="ChangeDisplay();">+25%
<br>
<h2 class="confirm">Does this look good? If so, please click the "Submit" button.</h2>
<span class="submit"><input type="submit"></span>
</form>
我有一个JavaScript函数应该根据所选的单选按钮更改图像:
function ChangeDisplay(){
switch(document.test.field.value){
case "short":
document.getElementById("testBox").innerHTML = "<img src='images/height/heightMinus25.gif'>";
break;
case "mid":
document.getElementById("testBox").innerHTML = "<img src='images/height/heightMid.gif'>";
break;
case "tall":
document.getElementById("testBox").innerHTML = "<img src='images/height/heightPlus25.gif'>";
break;
}
}
但是,当我选择按钮时,显示不会改变。任何帮助将不胜感激。
答案 0 :(得分:0)
在您的情况下,您应该获取单选按钮组的值,如下所示:
document.forms [0] .height.value
答案 1 :(得分:0)
另一种方法是使用JavaScript来更改背景图像。这是一个例子:
HTML
<div id="imgCont"></div><P>
<input type="radio" id="imgBtn" onclick="ClickMe();">Click Me
CSS
#imgCont {
background-image:
url('http://upload.wikimedia.org/wikipedia/commons/1/1b/Square_200x200.png');
background-repeat: no-repeat;
width: 200px;
height: 200px;
}
JAVASCRIPT
function ClickMe () {
if (document.getElementById("imgBtn").checked == true) {
的document.getElementById( “imgCont”)。style.backgroundImage = “URL(http://www.bizreport.com/2011/02/03/android-logo-200x200.jpg)” }
}
答案 2 :(得分:0)
尝试更改为以下内容:
onclick="ChangeDisplay(this.value);"
和
function ChangeDisplay(val){
switch(val){ ...
...