简体   繁体   English

将Bootstrap默认按钮更改为当前面板

[英]Change Bootstrap Default Button to Current Panel

Using Bootstrap, an application has a page with one form and multiple panels, each with a submit button. 使用Bootstrap,应用程序具有一个页面和一个面板,每个面板包含多个表单,每个面板都有一个提交按钮。 The first panel's button is the form default button whenever the Enter key is pressed. 只要按下Enter键,第一个面板的按钮就是表单默认按钮。 When a user fills in text inputs in the second panel and hits Enter, the second panel's button value should be sent to the Controller, but the first panel button's value, the form default button, is always sent instead. 当用户在第二个面板中填写文本输入并单击Enter时,应将第二个面板的按钮值发送到Controller,但始终会发送第一个面板按钮的值,即默认按钮形式。

I've tried adding a script to trigger focus or activation on the appropriate panel button when that panel is clicked or activated, to no avail. 我尝试添加脚本以在单击或激活该面板时触发相应面板按钮上的焦点或激活,但无济于事。 Does anyone know how to target a panel button for submission when it's panel is the one being used by the User? 当面板是用户正在使用的面板按钮时,有人知道如何将面板按钮作为提交目标吗? Thanks in advance! 提前致谢!

Snippet: 片段:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="https://bootswatch.com/bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/> <div class="row"> <div class="col-lg-12"> <div class="panel panel-default" id="basicsPanel"> <div class="panel-heading">Basic Info</div> <div class="panel-body"> <div class="row"> <!-- Text boxes and such --> </div> <div class="row"> <!-- This is always the default button upon pressing Enter key --> <button type="submit" id="saveBasics" name="buttonCommand" class="btn btn-success pull-right" value="Save Basics">Save Basics</button> </div> </div> </div> </div> </div> <div class="row"> <div class="col-lg-12"> <div class="panel panel-info" id="ownerPanel"> <div class="panel-heading">Owner</div> <div class="panel-body"> <div class="row"> <div class="col-lg-4"> <!-- Other text boxes and such --> </div> </div> <div class="row"> <div class="col-lg-12 text-right"> <!-- This button should be the one used when a User presses the Enter key while filling in data in this panel --> <button type="submit" id="saveOwner" name="buttonCommand" class="btn btn-success" value="Save Owner">Save Owner</button> </div> </div> </div> </div> </div> </div> 

Example of attempted script, which does indeed focus on the correct button when a panel is activated, but prevents the user from actually entering information into the panel's text boxes: 尝试脚本的示例,该脚本确实在面板被激活时专注于正确的按钮,但是阻止用户将信息实际输入到面板的文本框中:

$("#ownerPanel").click(function () {
    $("#saveOwner").focus();
});

$("#basicsPanel").click(function () {
    $("#saveBasics").focus();
});

As of HTML5, the default button is the first button defined . 从HTML5开始, 默认按钮是定义的第一个按钮 You have a couple options to handle multiple buttons with enter presses: 您可以通过以下两种选择来处理多个按钮:

  • Define multiple forms, each with one submit button. 定义多个表单,每个表单都有一个提交按钮。 That way each panel is tied directly to the button you want it to use. 这样,每个面板都直接绑定到您要使用的按钮。 This is nice and clean if the forms are independent, but if you still want the data from all forms, you'd need to catch the submission in JavaScript and pull the other forms' data into the payload. 如果表单是独立的,那么这很好,也很干净,但是如果您仍然想要所有表单中的数据,则需要捕获JavaScript中的提交并将其他表单的数据拉入有效负载中。
  • Capture the keypress event in JavaScript and use the currently focused field to determine which button should be pressed. 在JavaScript中捕获keypress事件,并使用当前关注的字段来确定应按下哪个按钮。 This lets you keep a single form with all the data in one place. 这样一来,您就可以将一个表格和所有数据都放在一个地方。 A simple strategy for picking the button to use might be to traverse from the focused field to .closest('panel') and then back down to children('input[type=submit]') . 挑选要使用的按钮的简单策略可能是从焦点区域遍历到.closest('panel') ,然后再返回到children('input[type=submit]')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM