你如何确定使用jQuery提交表单的提交? [英] How do you determine which submit was used to submit a form with jQuery?
问题描述
因此,考虑可以提交表单的所有方式,如何确定在提交内部使用哪个表单提交表单事件。我在这两个提交元素上有不同的名称。
触发提交按钮的任何操作—无论是实际的鼠标点击还是某些键盘动作 - mdash;仍然会触发一个点击事件,并且仍然会导致输入被包含为表单参数。
也就是说,如果您看到一个带有输入字段名称的表单参数,值,你知道那个提交按钮被点击了。
编辑—如果表单提交是因为您在文本字段中输入Enter而发生的,那么浏览器会选择第一个提交按钮(我认为这似乎是Firefox至少做的)。 (等待;从头开始; Firefox似乎在按下Enter键时有焦点的元素之后找到下一个submit输入...)
因此:
< form action ='whatever'method ='post'>
< input type ='text'name ='text'>
< input name ='submit1'value ='submit1'type ='submit'>
< input name ='submit2'value ='submit2'type ='submit'>
< / form>
在文本字段中输入Enter会导致submit1 = submit1成为表单参数,当submit1具有焦点时将会击中Enter。选择submit2并点击Enter会导致参数submit2 = submit2。
无论哪种情况,只有一个submit输入显示在参数列表中。
Keep in mind that the user does NOT have to click the submit nput. They could tab over to it and push enter.
So considering all ways a form can be submitted, how can you determine which one was used to submit the form inside the submit event. I have different names on the two submit elements.
Any action that triggers a submit button — be it an actual mouse click or some keyboard action — still triggers a "click" event and still causes the input to be included as a form parameter.
That is, if you see a form parameter with your input field's name and value, you know that that submit button was clicked.
edit — if the form submit happens because you hit "Enter" in a text field, the browser picks the first submit button (I think; that seems to be what Firefox does at least). (Wait; scratch that; Firefox seems to find the next "submit" input after the element that had focus when "Enter" was pressed ...)
Thus:
<form action='whatever' method='post'>
<input type='text' name='text'>
<input name='submit1' value='submit1' type='submit'>
<input name='submit2' value='submit2' type='submit'>
</form>
Hitting "Enter" in the text field would result in "submit1=submit1" being a form parameter, as would hitting "Enter" when "submit1" had focus. Tabbing to "submit2" and hitting "Enter" would result in "submit2=submit2" being among the parameters.
In either case, only one of the "submit" inputs shows up in the parameter list.
这篇关于你如何确定使用jQuery提交表单的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!