首頁 >web前端 >html教學 >form表单内的button标签问题_html/css_WEB-ITnose

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原創
2016-06-21 09:18:191482瀏覽

<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>


当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>

真的是啊


不能同名!

<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>


当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>



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

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

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



试了下,确实如此,牛!
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn