我有一个小型搜索表单,可以执行两种类型的搜索:
<form id="form-navbar-search" class="navbar-search pull-left" action="index.cfm?fuseaction=Search.output" method="post">
<div class="input-append">
<input type="text" name="header_search_criteria" title="Criteria to search" placeholder="search" class="search-query span9" />
<input type="submit" name="quick" value="Quick" title="Search on task ID or task name" class="btn btn-inverse" />
<a href="index.cfm?fuseaction=Search.home" id="navbar-search-full" title="Start a full search" class="btn btn-inverse" />Full</a>
</div>
</form>
- 如果按下“快速”提交按钮,表单将提交到默认操作属性 (
index.cfm?fuseaction=Search.output
)。这行得通。 - 如果按下“完整”按钮(实际上是
a
我使用 Bootstrap 将其设置为按钮的样式),我想阻止链接打开新页面,将表单的操作更改为index.cfm?fuseaction=Search.home
,然后提交表单。我为此编写了这个 jQuery:
$('#navbar-search-full').click( function(event) {
event.preventDefault(); //don't let the link open a new page
$('#form-navbar-search').attr('action', $(this).attr('href')).submit(); //instead, change the search form action, then submit it
})
.attr('href','#navbar-search-full');
正如您在此 JSFiddle 中看到的, preventDefault() 似乎没有拦截链接操作。我需要做哪些调整才能做到这一点?