我有以下html -
<input type="Radio" name="radio2text" value="Radiobutton1"
onclick="javascript:radioWithText('one')" checked="checked" />OneRadio<br/>
<input type="Radio" name="radio2text" value="Radiobutton2"
onclick="javascript:radioWithText('two')" unchecked />TwoRadio
<br/>
<input type="Radio" name="radio2text" value="Radiobutton2"
onclick="javascript:radioWithText('three')" unchecked />ThreeRadio
<br/>
<input type="Radio" name="radio2text" value="Radiobutton2"
onclick="javascript:radioWithText('four')" unchecked />FourRadio
还有这个
<span id="one" name="one" width="5px" style="background-color:#fff;"> </span>
<span id="two" name="one" width="5px" style="background-color:#fff;"> </span>
<span id="three" name="one" width="5px" style="background-color:#fff;"> </span>
<span id="four" name="one" width="5px" style="background-color:#fff;"> </span>
这会在我的背景颜色为绿色时产生4个小宽度的正方形。显示了这四个方块,我想在选择特定单选按钮时将其颜色偏离背景的背景更改为红色。对于那次震动,我通过javascript javascript:radioWithText('one')
传递了一个参数。正如您所看到的,函数传递参数ok!接下来是用我调用的函数做javascript。
function radioWithText(d) {
alert(d);
if (d.val=='one')
{
var one = document.getElementById('one');
//one.checked = true;
one.style.background="red";
}
}
您可以看到,当选中单选按钮时,它会警告传递的值(选中的按钮)。在提醒时它显示"one"
"two"
"three"
"four"
,而第一个单选按钮放置时的相同值通过"one"
时进行比较,而不是比较可能的内容原因?帮助我!
事先感谢一个愚蠢的错误
答案 0 :(得分:1)
首先从内联事件中删除javascript:
:
<input type="Radio" name="radio2text" value="Radiobutton1"
onclick="radioWithText('one')" checked="checked" />OneRadio<br/>
<input type="Radio" name="radio2text" value="Radiobutton2"
onclick="radioWithText('two')" unchecked />TwoRadio
<br/>
<input type="Radio" name="radio2text" value="Radiobutton2"
onclick="radioWithText('three')" unchecked />ThreeRadio
<br/>
<input type="Radio" name="radio2text" value="Radiobutton2"
onclick="radioWithText('four')" unchecked />FourRadio
其次,您将字符串传递给函数,因此没有.val
属性。删除:
function radioWithText(d) {
alert(d);
if (d == 'one')
{
var one = document.getElementById('one');
//one.checked = true;
one.style.background="red";
}
}
答案 1 :(得分:1)
删除.val
。您的参数是一个字符串,没有.val
function radioWithText(d) {
alert(d);
if (d == 'one')
{
var one = document.getElementById('one');
//one.checked = true;
one.style.background="red";
}
}
答案 2 :(得分:1)
变量d
包含您需要比较的值。它没有名为val
的属性。
比较线应为:
if (d === 'one')
答案 3 :(得分:0)
不需要Val。
function radioWithText(d) {
alert(d);
if (d == 'one')
{
var one = document.getElementById('one');
//one.checked = true;
one.style.background="red";
}
}
答案 4 :(得分:0)
您不需要d.val
d
变量等于'one'
。
function radioWithText(d) {
alert(d);
if (d=='one')
{
var one = document.getElementById('one');
//one.checked = true;
one.style.background="red";
}
}
此外,您的javascript:
活动中不需要onclick
。与onclick
元素不同,a
直接映射到javascript。