我想要一个函数从表单中获取一些值作为输入并在后台进行操作。我尝试过,但它转到 url mydomain.com/gtype=selected
value 。我只是想让它在后台做一些事情而不是改变 url
我这里有一个 fiddle 。 http://jsfiddle.net/4qppX/
最佳答案
您需要在 onclick
处理程序中返回 false
。
<button onclick="hihi(this.form.gtype); return false">sub</button>
另一种方法是将按钮设为 non-submitting button通过说 type="button"
<button type="button" onclick="hihi(this.form.gtype);">sub</button>
我不确定 W3C states 这样做的原因:
If the element has a form owner, the element must submit the form owner from the button element.
关于javascript - 使用 <button> 标签选择性提交表单不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6110253/