Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

javascript - 我可以<button>不提交表格吗?</button>(Can I make a <button> not submit a form?)

I've got a form, with 2 buttons(我有一个表格,有两个按钮)

<a href="index.html"><button>Cancel changes</button></a> <button type="submit">Submit</button> I use jQuery UI's button on them too, simply like this(我也使用jQuery UI的按钮,就像这样) $('button').button(); However, the first button also submits the form.(但是,第一个按钮也会提交表单。) I would have thought that if it didn't have the type="submit" , it wouldn't.(我本以为如果没有type="submit" ,就没有。) Obviously I could do this(显然我可以做到这一点) $('button[type!=submit]').click(function(event) { event.stopPropagation(); }); But is there a way I can stop that back button from submitting the form without JavaScript intervention?(但是有没有办法可以在没有JavaScript干预的情况下阻止后退按钮提交表单?) To be honest, I used a button only so I could style it with jQuery UI.(老实说,我只使用了一个按钮,因此可以使用jQuery UI设置样式。) I tried calling button() on the link and it didn't work as expected (looked quite ugly!).(我尝试在链接上调用button() ,但未按预期工作(看起来很丑!)。)   ask by alex translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The default value for the type attribute of button elements is "submit".(为默认值type的属性button元素 “提交”。)

Set it to type="button" to produce a button that doesn't submit the form.(将其设置为type="button"以产生不提交表单的按钮。) <button type="button">Submit</button> In the words of the HTML Standard : "Does nothing."(用HTML标准的话说:“什么都不做。”)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...