【问题标题】:Button not submitting on enter按钮未在输入时提交
【发布时间】:2016-07-14 05:55:25
【问题描述】:

我正在使用搜索表单,我必须单击按钮才能提交搜索。我想要的是在按回车时进行搜索,但它没有任何建议。

     <button class="btn btn-default" style="height:34px" type="submit" onclick="Search();"><span class="glyphicon glyphicon-search"></span></button>

这是我的表格

    <input type="hidden" name="search_param" value="all" id="search_param">   
              @Html.TextBoxFor(m => m.SearchTrm, new { @class="form-control",id="Searchterm",placeholder="Search for Drug, Disease or Sponsor"})  

            <span class="input-group-btn">
          <button class="btn btn-default" id="btn-default" style="height:34px" type="button" onclick="Search();"><span class="glyphicon glyphicon-search"></span></button>

            </span>

【问题讨论】:

  • 在表单标签内吗?
  • 更改 type="submit" 为 type="button" 并测试它
  • 可以出示您的表格吗?
  • 不,我没有使用表单标签。这就是为什么?

标签: javascript html


【解决方案1】:

要使用 enter 提交表单,您必须有一个表单,并且在按下 enter 时表单控件必须具有焦点(textarea 元素除外,其中 enter 将插入一个回车符)。您必须将按钮的单击处理程序移动到表单的提交处理程序(否则它不会被调用)。

表单中的按钮元素默认为提交按钮。

例如

function search(form) {
  alert('You are searching on: ' + form.searchText.value);
  // do stuff 
}
<form onsubmit="search(this)">
  Search text: <input name="searchText">
  <button>Do search</button>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多