我有一个包含两个提交按钮的表单,name="submit_button"
和name="search_me_logo"
,当表单到达action
位置时,根据按下的按钮,它会执行两个不同的操作。
当按下回车键时,我希望它与name="submit_button"
做同样的事情,但现在它似乎默认发送name="search_me_logo"
。
以下是您需要的HTML代码:
<form action="/search_me.php" method="GET">
<div id="search_me_outer_div">
<button id="search_me_div" name="search_me_logo" type="submit">
<img id="search_me_image" src="/images/header/search_icons/search_me.png" height="33" alt='"search me"'/>
</button>
</div><!--
--><div id="search_box_outer_div">
<div id="search_box_div">
<input id="search_box" onfocus="hidePlaceholderAndShineBox();" onblur="showPlaceholderAndBlurBox();" name="search" type="text" spellcheck="false" size="32" placeholder='Get to know Sam... "search me"'>
<button id="search_div" name="submit_button" type="submit">
<img id="search_img" src="images/header/search_icons/fancy_search.png" height="21" alt="Go"/>
</button>
</div>
</div>
</form>
PHP:
if (isset($_GET['submit_button'])) {
echo 'submit was pressed<br>';
} else if (isset($_GET['search_me_logo'])) {
echo 'logo was pressed<br>';
} else if (isset($_GET['search'])) {
echo 'enter was pressed<br>';
} else {
//show error page
}
现在当我按下回车键时,按下echo
s“徽标”。可能有一种使用JavaScript的方法,但如果只使用HTML / PHP就可以了,那就太棒了。
答案 0 :(得分:2)
默认情况下,按Enter键将导致第一个提交按钮。您只需在表单开头的隐藏div中添加默认提交操作即可。例如:
<form action="/search_me.php" method="GET">
<div style="height:0px; width:0px; overflow:hidden;"><button id="search_div" name="submit_button" type="submit"></button></div>
并保持原样。
编辑:如果某个浏览器没有显示,某些浏览器不会让enter键触发第一个按钮(例如display:none;)。
但它适用于:
width: 0;
height: 0;
overflow: hidden;
在包含隐藏提交按钮的元素的CSS中。
答案 1 :(得分:0)
感谢所有建议。 @ NoGray的答案可行,或者我只是做了两种形式,正如@Irdrah建议的那样。
每个表单的按钮和输入都有不同的名称,其中一个输入有type="hidden"
和id="hidden_input"
。然后,当点击带有隐藏输入的表单的提交时,我使用jquery的submit()
来设置
document.getElementById('hidden_input').value = document.getElementById('shown_input').value;`
和return
ed true
。我没有试过@NoGray,但我相信它会起作用。