简体   繁体   English

JavaScript不比较传递的参数值

[英]JavaScript not comparing the parameter value passed

I have following html - 我有以下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

and also this 还有这个

<span id="one" name="one" width="5px" style="background-color:#fff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span id="two" name="one" width="5px" style="background-color:#fff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span id="three" name="one" width="5px" style="background-color:#fff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span id="four" name="one" width="5px" style="background-color:#fff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>

This produces a 4 small width square in white on my background color of green. 这在我的背景颜色为绿色时产生4个小宽度正方形。 Those four square are shown and i want to change their color off-course background to be red when particular radio button is selected. 显示了这四个方块,我想在选择特定单选按钮时将其颜色偏离背景的背景更改为红色。 For that shake i pass a paramter with javascript javascript:radioWithText('one') . 为了那个震动,我通过javascript javascript:radioWithText('one')传递了一个参数javascript:radioWithText('one') As you can see the function is passed a parameter ok! 正如您所看到的,函数传递参数ok! Next is to do javascript with the function i called. 接下来是用我调用的函数做javascript。

function radioWithText(d) {
    alert(d);
    if (d.val=='one')
    {
        var one = document.getElementById('one');
        //one.checked = true;
        one.style.background="red";
    }
}

You can see probably when the radio button is checked it alert the value passed (the button which is selected). 您可以看到,当选中单选按钮时,它会警告传递的值(选中的按钮)。 In alert it show "one" "two" "three" "four" while the same value that is when first radio button is placed passes "one" when compared it not comparing what may be the reason? 在警报中,它显示"one" "two" "three" "four"而第一个单选按钮放置时的相同值通过"one"时比较它不比较可能的原因? help me out! 帮帮我!
Desperately a silly mistake thanks in advance 事先感谢一个愚蠢的错误

First remove javascript: from the inline events: 首先从内联事件中删除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

Secondly, you're passing a string to your function, so there is no .val property. 其次,您将一个字符串传递给您的函数,因此没有.val属性。 Remove that: 删除:

function radioWithText(d) {
    alert(d);
    if (d == 'one')
    {
        var one = document.getElementById('one');
        //one.checked = true;
        one.style.background="red";
    }
}

Remove the .val . 删除.val Your parameter is a string, there is no .val 你的参数是一个字符串,没有.val

function radioWithText(d) {
    alert(d);
    if (d == 'one')
    {
        var one = document.getElementById('one');
        //one.checked = true;
        one.style.background="red";
    }
}

The variable d holds the value you need to compare. 变量d包含您需要比较的值。 It does not have a property named val . 它没有名为val的属性。

The comparison line should be: 比较线应该是:

if (d === 'one')

No Val needed. 不需要Val。

function radioWithText(d) {
    alert(d);
    if (d == 'one')
    {
        var one = document.getElementById('one');
        //one.checked = true;
        one.style.background="red";
    }
}

You don't need the d.val 你不需要d.val

The d variable is equal to 'one' . d变量等于'one'

function radioWithText(d) {
alert(d);
if (d=='one')
{
    var one = document.getElementById('one');
    //one.checked = true;
    one.style.background="red";
    }
}

Also, no need for the javascript: in your onclick events. 此外,在你的onclick事件中不需要javascript: onclick maps directly to javascript, unlike a elements. onclick直接映射到的JavaScript,不像a元素。

Fiddle here. 在这里小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM