我想创建一个包含多个提交链接按钮的表单。我知道可以通过使用并指定 <button>
的名称来完成或<input type="button">
像这样的东西:
在 HTML 中:
<form action="" method="get">
Other form elements here...
<button type="submit" name="activated">Activated</button>
<button type="submit" name="pending">Pending</button>
<button type="submit" name="suspended">Suspended</button>
</form>
在 PHP 中:
<?php
if(isset($_GET["activated"])) {
Activated codes here...
}
elseif(isset($_GET["pending"])) {
Pending codes here...
}
elseif(isset($_GET["suspended"])) {
Suspended codes here...
}
?>
我希望使用链接来完成提交按钮,而不是<button>
或<input type="submit">
像这样的东西:
<a href="#">Activated</a>
<a href="#">Pending</a>
<a href="#">Suspended</a>
我听说可以使用 JavaScript 或 JQuery 来完成,但我不知道怎么做,有人知道吗?
更新: 例如,我想要发生的是,当我单击“已激活”链接时,我只想处理 isset($_GET["activated"])
下的逻辑。 .
背后的原因:
我想要使用提交链接按钮而不是普通提交按钮标签的原因是,我想使用此 Bootstrap 下拉按钮样式来更改表上用户的状态:
它是基于链接的,所以这就是原因。
PS:抱歉英语不好,不是我的母语。
最佳答案
您可以在 anchor 上使用数据属性,然后将该属性加载到隐藏字段中以 checkin PHP 代码。
<form action="" method="post">
<a href="#" class="anchor-btn" data-name="activated">Activated</a>
<a href="#" class="anchor-btn" data-name="pending">Pending</a>
<a href="#" class="anchor-btn" data-name="suspended">Suspended</a>
<input type="hidden" id="actionName" name="actionName" value="" />
</form>
$('.anchor-btn').click(function(e) {
e.preventDefault();
$('#actionName').val($(this).data('name'));
$('form').submit();
});
<?php
if($_POST['actionName'] == "activated") {
Activated code goes here
}
...etc.
?>
关于javascript - "link"一种表单中存在多个提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756624/