Identifying main submit button for a form

2019-03-03 17:29发布

问题:

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"/>