开发者

Java Script works with a single value but not with two

I have a Java Script that stops working when I try to input two values. This is the script that works:

<HEAD> <script type="text/javascript">
function addsuggest(name)
{
document.getElementById("recipients").innerHTML=document.getElementById("recipients").innerHTML+name+".L ";
}

</script> </HEAD>
<a href=javascript:onClick=addsuggest('3') >click here<a/><br>
<span id=recipients  style="color:blue;"> </span>

Nothing fancy. It just adds 3.L in blue color to the end of a line. I want this function to do some other things so I change 开发者_运维问答function addsuggest(name) to function addsuggest(name, id) and onClick=addsuggest('3') toonClick=addsuggest('3', 'John')

These are the symptoms I have:

  1. It won't work. I tried making it a button. Again it won't work.
  2. When I hover over the link it shows javascript:onClick=addsuggest('3' and not the rest. So it's stopped reading it at the comma.
  3. Rest of the page content goes blue as if it's link but not clickable.

This is the simplest thing I've done with Java Script. It's just inputting values and then printing it. So, what's the matter?


You need quotes surrounding the href attribute (and should place them around all other attributes), otherwise the single quotes around the function parameters will break the link:

<HEAD> <script type="text/javascript">
function addsuggest(name, id)
{
document.getElementById("recipients").innerHTML=document.getElementById("recipients").innerHTML+name+id+".L ";
}

</script> </HEAD>
<a href="javascript:onClick=addsuggest('3', 'John');" >click here<a/><br>
<span id="recipients"  style="color:blue;"> </span>


<a href="#" onclick="javascript:addsuggest('3');">...</a>

and for readability:

function addsuggest(name)
{
    document.getElementById("recipients").innerHTML += name + ".L";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜