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

onClick JavaScript function inside HTML-form - Stack Overflow

programmeradmin1浏览0评论

I cant't get a simple button to work when it is inside a html form, while it works normally when not inside a html form.

This code doesn't work:

<head>
    <script langauge="JavaScript">
    function reset(){
        alert('test')
    }
    </script>
</head>

<body>
<form name="nyform">
    <input type="button" value="Reset" onClick="reset()">
</form>
</body>

While this one does:

<head>
    <script langauge="JavaScript">
    function reset(){
        alert('test')
    }
    </script>
</head>

<body>
    <input type="button" value="Reset" onClick="reset()">
</body>

What is the reason for this, and how can I correct it?

Thanks

I cant't get a simple button to work when it is inside a html form, while it works normally when not inside a html form.

This code doesn't work:

<head>
    <script langauge="JavaScript">
    function reset(){
        alert('test')
    }
    </script>
</head>

<body>
<form name="nyform">
    <input type="button" value="Reset" onClick="reset()">
</form>
</body>

While this one does:

<head>
    <script langauge="JavaScript">
    function reset(){
        alert('test')
    }
    </script>
</head>

<body>
    <input type="button" value="Reset" onClick="reset()">
</body>

What is the reason for this, and how can I correct it?

Thanks

Share Improve this question asked Nov 12, 2012 at 16:14 user1626227user1626227 1233 silver badges9 bronze badges 2
  • 4 Maybe change your function's name.. I am thinking that reset may be a reserved word. – carny666 Commented Nov 12, 2012 at 16:17
  • He is right, reset() is a JavaScript-function which clears/empties form fields. The second sample just works because no form-tag is wrapped. – Marco Francke Commented Nov 12, 2012 at 16:22
Add a ment  | 

1 Answer 1

Reset to default 5

This happens because the function reset() has a different meaning inside a form, and your custom function doesn't override it. I changed the function name to reset2 and it all worked as expected.

发布评论

评论列表(0)

  1. 暂无评论