Home > Web Front-end > HTML Tutorial > form表单内的button标签问题_html/css_WEB-ITnose

form表单内的button标签问题_html/css_WEB-ITnose

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-21 09:18:19
Original
1469 people have browsed it

<form>        <input type="text" id='username'>        <button type="button" id='save' onclick="save()">保存</button>    </form> <script>        function save(){            var name = document.getElementById('username').value;            alert(name);            if(name=='hello'){                history.go(-1);            } else{                alert('输入错误 hello');            }        }    </script>
Copy after login
Copy after login


当button放在form表单里的时候,onclick事件就没有响应,不放在form表单里时,save()方法就得到调用了。
请问,这是什么情况呢?是自动提交了表单,而没有响应吗?


回复讨论(解决方案)

函数名称换个

<form>        <input type="text" id='username'>        <button type="button" id='save' onclick="save1()">保存</button>    </form>  <script>        function save1(){            var name = document.getElementById('username').value;            alert(name);             if(name=='hello'){                history.go(-1);            } else{                alert('输入错误 hello');            }        }    </script>
Copy after login
Copy after login

真的是啊


不能同名!

<form>        <input type="text" id='username'>        <button type="button" id='save' onclick="save()">保存</button>    </form> <script>        function save(){            var name = document.getElementById('username').value;            alert(name);            if(name=='hello'){                history.go(-1);            } else{                alert('输入错误 hello');            }        }    </script>
Copy after login
Copy after login


当button放在form表单里的时候,onclick事件就没有响应,不放在form表单里时,save()方法就得到调用了。
请问,这是什么情况呢?是自动提交了表单,而没有响应吗?



可是我把button不放在form表单里,就有响应了,这又是为什么呢?



函数名称换个

<form>        <input type="text" id='username'>        <button type="button" id='save' onclick="save1()">保存</button>    </form>  <script>        function save1(){            var name = document.getElementById('username').value;            alert(name);             if(name=='hello'){                history.go(-1);            } else{                alert('输入错误 hello');            }        }    </script>
Copy after login
Copy after login



可是我把button不放在form表单里,就有响应了,这又是为什么呢?
 
        
        
     

在form里save被当成了 onclick="console.log(save);" 把save打印出来看下就知道了

在form里save被当成了 onclick="console.log(save);" 把save打印出来看下就知道了



试了下,确实如此,牛!
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template