一个郁闷了好久的错误:
<form method="get">
<input type="text" name="search" id="search" />
<input type="submit" value="submit" id="submit" />
</form>
$("#submit").submit(function(){
alert("hello");
return false;
});
问题是无论点击多次次,都不会运行.在官网找到了答案:
.submit( )
This method is a shortcut for .on('submit', handler)
in the first variation, and .trigger('submit')
in the third.
The submit
event is sent to an element when the user is attempting to submit a form. It can only be attached to <form>
elements. Forms can be submitted either by clicking an explicit <input type="submit">
, <input type="image">
, or<button type="submit">
, or by pressing Enter when certain form elements have focus.
Depending on the browser, the Enter key may only cause a form submission if the form has exactly one text field, or only when there is a submit button present. The interface should not rely on a particular behavior for this key unless the issue is forced by observing the keypress event for presses of the Enter key.
官网已经说的很清楚了,这个.submit只能用于form元素,而不能用于submit,button等。
改为$("form").submit 后就ok了。