Identifying main submit button for a form

▼魔方 西西 提交于 2019-12-20 06:31:00

问题


I have a form which has a few text boxes, and two submit buttons. When a text box is selected, and I press Enter, the first submit button is triggered. However, this is not the button that should be pressed at that time. How can I make it so that when the Enter button is pressed, the second submit button (or at least, the last occurrence of the submit button) clicked?

I have an implementation of this currently using JavaScript, but I'm hoping there is a native option for it.


回答1:


Its pretty tough to get more native than javascript. You're gonna need it here. By the way, the behavior you noticed is the expected behavior. The first submit button in your html is the one that will get sent with your header.

I guess if you really didn't want to use js, you could just put the other button first and then use css to position them correctly on the page itself.




回答2:


If you are using asp .net then you can use "UseSubmitBehavior" property on the button. See below code.

<asp:TextBox runat="server" ID="txt1"></asp:TextBox> <br/>
    <asp:Button runat="server" ID="btn1" UseSubmitBehavior="False" Text="1" OnClick="test1"/> <br/>
    <asp:Button runat="server" ID="btn2" UseSubmitBehavior="True" Text="2" OnClick="test2"/>


来源:https://stackoverflow.com/questions/12082481/identifying-main-submit-button-for-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!