最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

HTML Form onclick event doesn't call javascript function - Stack Overflow

programmeradmin0浏览0评论

I would like to create a dynamic form, adding inputs into the form by pressing a button (or an image) when I put the code directly on the onclick event

alert("Hello");

it works... however, it doesn't when I try to call a javascript function... Even If I call the simple function hello(); (as well with add() )

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function add() {
    var container = document.createElement("div"); 
    container.innerHTML = "<input type='text' name='email'><br/>";
    document.getElementById('emails').appendChild(container);
}
function hello() {
    alert("Hello"); 
}
​</script>
</head>
<body>
<form id="myform" name="myform" action="valid.php" method="POST">
    <div id="emails">
        <input type='text' name='email' value=''/><br />
    </div>
    <img width="50px" src="myimage.jpg" onclick='add();'/>
    <input type="submit" value="Submit"/>
</form>
</body>
</html>

Anyone manage to spot why ?

I would like to create a dynamic form, adding inputs into the form by pressing a button (or an image) when I put the code directly on the onclick event

alert("Hello");

it works... however, it doesn't when I try to call a javascript function... Even If I call the simple function hello(); (as well with add() )

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function add() {
    var container = document.createElement("div"); 
    container.innerHTML = "<input type='text' name='email'><br/>";
    document.getElementById('emails').appendChild(container);
}
function hello() {
    alert("Hello"); 
}
​</script>
</head>
<body>
<form id="myform" name="myform" action="valid.php" method="POST">
    <div id="emails">
        <input type='text' name='email' value=''/><br />
    </div>
    <img width="50px" src="myimage.jpg" onclick='add();'/>
    <input type="submit" value="Submit"/>
</form>
</body>
</html>

Anyone manage to spot why ?

Share Improve this question edited Sep 20, 2012 at 17:35 TheSquad asked Sep 20, 2012 at 17:27 TheSquadTheSquad 7,5068 gold badges41 silver badges80 bronze badges 7
  • Where do you actually call the function? – Shomz Commented Sep 20, 2012 at 17:28
  • I see no place where you specify hello as onclick event handler – Viktor S. Commented Sep 20, 2012 at 17:30
  • Seems to work here jsfiddle/j08691/k9g5e – j08691 Commented Sep 20, 2012 at 17:30
  • calling hello() or add(), it doesn't work... – TheSquad Commented Sep 20, 2012 at 17:33
  • Both work fine in @j08691's example. – Shomz Commented Sep 20, 2012 at 17:35
 |  Show 2 more ments

2 Answers 2

Reset to default 3

Looks like it have some kind of special character before:

 </script>.

I just copied and pasted Here. Look the first character in the 13th line

I copied this into a nice little jsfiddle here http://jsfiddle/dr9hG/

I also changed the name from just "email" to "email[]" so that you can access all of them by array on the page this submits to.

发布评论

评论列表(0)

  1. 暂无评论