假设您在HTML表单中创建了一个向导。一个按钮后退,一个按钮前进。因为当您按Enter键时,返回按钮首先出现在标记中,它将使用该按钮提交表单。
例子:
< >形式
<!—将光标移至该字段,按“Enter”。—>
<input type="text" name="field1" />
<!这是提交的按钮——>
<input type="submit" name="prev" value="Previous Page" />
<!——但这是我想提交的按钮——>
<input type="submit" name="next" value=" next Page" />
> < /形式
我想要确定当用户按Enter键时使用哪个按钮提交表单。这样,当您按下Enter键时,向导将移动到下一页,而不是上一页。你必须使用tabindex来做这个吗?
这是我尝试过的:
你需要确保你给你的按钮不同的名字
编写if语句,在单击任意一个按钮时执行所需的操作。
<form>
<input type="text" name="field1" /> <!-- Put your cursor in this field and press Enter -->
<input type="submit" name="prev" value="Previous Page" /> <!-- This is the button that will submit -->
<input type="submit" name="next" value="Next Page" /> <!-- But this is the button that I WANT to submit -->
</form>
在PHP中,
if(isset($_POST['prev']))
{
header("Location: previous.html");
die();
}
if(isset($_POST['next']))
{
header("Location: next.html");
die();
}
与其纠结于多次提交,JavaScript或类似的东西来做一些上/下的事情,另一种选择是使用旋转木马来模拟不同的页面。
这样做:
你不需要多个按钮,输入或提交来做上一个/下一个事情,你只有一个输入类型="submit"在只有一个形式。
整个表单中的值一直存在,直到表单提交为止。
用户可以切换到任何上一页和下一页来完美地修改值。
使用Bootstrap 5.0.0的示例:
<div id="carousel" class="carousel slide" data-ride="carousel">
<form action="index.php" method="post" class="carousel-inner">
<div class="carousel-item active">
<input type="text" name="lastname" placeholder="Lastname"/>
</div>
<div class="carousel-item">
<input type="text" name="firstname" placeholder="Firstname"/>
</div>
<div class="carousel-item">
<input type="submit" name="submit" value="Submit"/>
</div>
</form>
<a class="btn-secondary" href="#carousel" role="button" data-slide="prev">Previous page</a>
<a class="btn-primary" href="#carousel" role="button" data-slide="next">Next page</a>
</div>
当我第一次遇到这种情况时,我想出了一个onclick()/JavaScript hack,当选择不是prev/next时,我仍然喜欢它的简单性。它是这样的:
@model myApp.Models.myModel
<script type="text/javascript">
function doOperation(op) {
document.getElementById("OperationId").innerText = op;
// you could also use Ajax to reference the element.
}
</script>
<form>
<input type="text" id = "TextFieldId" name="TextField" value="" />
<input type="hidden" id="OperationId" name="Operation" value="" />
<input type="submit" name="write" value="Write" onclick='doOperation("Write")'/>
<input type="submit" name="read" value="Read" onclick='doOperation("Read")'/>
</form>
当单击任何一个提交按钮时,它将所需的操作存储在一个隐藏字段中(该字段是包含在与表单关联的模型中的字符串字段),并将表单提交给控制器,由控制器进行所有决定。在控制器中,你只需写:
// Do operation according to which submit button was clicked
// based on the contents of the hidden Operation field.
if (myModel.Operation == "Read")
{
// Do read logic
}
else if (myModel.Operation == "Write")
{
// Do write logic
}
else
{
// Do error logic
}
您还可以使用数值操作代码来避免字符串解析,但是除非使用枚举,否则代码可读性较差、可修改性较差、自文档性较差,解析也很简单。
我用这种方法解决了一个非常相似的问题:
If JavaScript is enabled (in most cases nowadays) then all the submit buttons are "degraded" to buttons at page load via JavaScript (jQuery). Click events on the "degraded" button typed buttons are also handled via JavaScript.
If JavaScript is not enabled then the form is served to the browser with multiple submit buttons. In this case hitting Enter on a textfield within the form will submit the form with the first button instead of the intended default, but at least the form is still usable: you can submit with both the prev and next buttons.
工作的例子:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<form action="http://httpbin.org/post" method="post">
If JavaScript is disabled, then you CAN submit the form
with button1, button2 or button3.
If you press enter on a text field, then the form is
submitted with the first submit button.
If JavaScript is enabled, then the submit typed buttons
without the 'defaultSubmitButton' style are converted
to button typed buttons.
If you press Enter on a text field, then the form is
submitted with the only submit button
(the one with class defaultSubmitButton)
If you click on any other button in the form, then the
form is submitted with that button's value.
<br />
<input type="text" name="text1" ></input>
<button type="submit" name="action" value="button1" >button 1</button>
<br />
<input type="text" name="text2" ></input>
<button type="submit" name="action" value="button2" >button 2</button>
<br />
<input type="text" name="text3" ></input>
<button class="defaultSubmitButton" type="submit" name="action" value="button3" >default button</button>
</form>
<script>
$(document).ready(function(){
/* Change submit typed buttons without the 'defaultSubmitButton'
style to button typed buttons */
$('form button[type=submit]').not('.defaultSubmitButton').each(function(){
$(this).attr('type', 'button');
});
/* Clicking on button typed buttons results in:
1. Setting the form's submit button's value to
the clicked button's value,
2. Clicking on the form's submit button */
$('form button[type=button]').click(function( event ){
var form = event.target.closest('form');
var submit = $("button[type='submit']",form).first();
submit.val(event.target.value);
submit.click();
});
});
</script>
</body>
</html>