232

我想在我的表单输入提交按钮上使用 twitter 引导图标。

http://twitter.github.com/bootstrap/base-css.html#icons上的示例主要显示样式化的超链接。

我最接近的是让图标显示在按钮旁边,但不在里面。

<div class="input-prepend">
   <span class="add-on"><i class="icon-user icon-white"></i></span>
   <input type="submit" class="btn-primary" value="Login" >
</div>
4

12 回答 12

407

您可以使用按钮标签而不是输入

<button type="submit" class="btn btn-primary">
  <i class="icon-user icon-white"></i> Sign in
</button>
于 2012-02-22T11:33:46.343 回答
68

我认为您可以为此目的使用标签标签。以下是 twitter 引导 HTML 导航栏的示例:

<form class="navbar-search">
    <input type="text" class="search-query" placeholder="Search here" />        
    <label for="mySubmit" class="btn"><i class="icon-search icon-white"></i> Search me</label>
    <input id="mySubmit" type="submit" value="Go" class="hidden" />
</form>

基本上你会得到一个输入的标签元素(type=submit),然后你隐藏实际的输入提交。用户可以单击标签元素,仍然可以完成表单提交。

于 2012-09-15T07:48:01.037 回答
14

我认为你应该试试这个设计用于 Twitter Bootstrap 的FontAwesome 。

<button class="btn btn-primary icon-save">Button With Icon</button>
于 2012-07-17T07:01:45.913 回答
11

您可以在某处添加带有图标的 <a/>,并将 JavaScrit 操作绑定到它,以提交表单。如有必要,原始提交按钮的名称+值的名称和值可以在隐藏属性中。使用 jQuery 很容易,请允许我避免使用纯 JavaScript 版本。

假设这是原始形式:

<form method="post" id="myFavoriteForm>
   ...other fields...
   <input class="btn btn-primary" type="submit" name="login" value="Let me in" />
</form>

像这样改变它:

<form method="post" id="myFavoriteForm">
   ...other fields...
   <a href="#" class="btn btn-primary" id="myFavoriteFormSubmitButton">
      <i class="icon-user icon-white"></i>&nbsp;Let me in
   </a>
</form>

...然后是神奇的 jQuery:

$("#myFavoriteFormSubmitButton").bind('click', function(event) {
   $("#myFavoriteForm").submit();
});

或者,如果您想确保用户始终可以提交表单——这就是我会在你的鞋子里做的——,您可以将普通的提交按钮留在表单中,并使用 jQuery .hide() 隐藏它。它确保根据正常的提交按钮(有些人使用链接、w3m 和类似浏览器),在没有 JavaScript 和 jQuery 的情况下仍然可以登录,但如果可能的话,会提供一个带有图标的精美按钮。

于 2012-04-01T22:20:13.187 回答
8

bootstrap 3有一种新方法可以做到这一点:

<button type="button" class="btn btn-default btn-lg">
  <span class="glyphicon glyphicon-star"></span> Star
</button>

它位于“如何使用”下的引导 glyphicons页面上:

于 2014-05-06T23:30:57.543 回答
7

我想将 Twitter Bootstrap 图标转换为一个基本的 Rails 表单,并遇到了这篇文章。经过一番搜索,我想出了一个简单的方法来做到这一点。不确定您是否使用 Rails,但这里是代码。关键是将一个块传递给 link_to 查看助手:

<tbody>
    <% @products.each do |product| %>
      <tr>
        <td><%= product.id %></td>
        <td><%= link_to product.name, product_path(product) %></td>
        <td><%= product.created_at %></td>
        <td>
          <%= link_to(edit_product_path(product), :class => 'btn btn-mini') do %>
          Edit <i class="icon-pencil"></i>
          <% end %>

          <%= link_to(product_path(product), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger') do %>
          Delete <i class="icon-trash icon-white"></i>
          <% end %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

<%= link_to(new_product_path, :class => 'btn btn-primary') do %>
    New <i class="icon-plus icon-white"></i>
<% end %>

如果您不使用 Rails,这里是带有图标的链接的输出 HTML(用于编辑、删除和新提交按钮)

# Edit
<a href="/products/1/edit" class="btn btn-mini">Edit <i class="icon-pencil"></i></a>

# Delete
<a href="/products/1" class="btn btn-mini btn-danger" data-confirm="Are you sure?" data-method="delete" rel="nofollow"> Delete <i class="icon-trash icon-white"></i></a>

# New
<a href="/products/new" class="btn btn-primary">New <i class="icon-plus icon-white"></i></a>

这是完成结果的屏幕截图的链接:http: //grab.by/cVXm

于 2012-04-04T17:11:56.053 回答
6

有一种方法,如何将(引导程序的)字形图标放入input type="submit". 使用css3多背景。

HTML:

<form class="form-search">
   …
   <input type="submit" value="Search">
</form>

和 CSS:

.form-search input[type="submit"] {
    padding-left:16px; /* space for only one glyphicon */
    background:-moz-linear-gradient(top,#fff 0%,#eee 100%) no-repeat 16px top,
        url(../images/glyphicons-halflings.png) no-repeat -48px 0,
        -moz-linear-gradient(top,#fff 0%,#eee 100%); /* FF hack */
    /* another hacks here  */
    background:linear-gradient(to bottom,#fff 0%,#eee 100%) no-repeat 16px top,
        url(../images/glyphicons-halflings.png) no-repeat -48px 0,
        linear-gradient(to bottom,#fff 0%,#eee 100%); /* Standard way */
}

如果多个背景重叠,顶部将是background:符号的第一个背景。所以顶部是16px从左侧缩进的背景(16px是单个字形图标的宽度),底部是整体glyphicons-halflings.png,底部(覆盖整个元素)是与顶层相同的背景渐变。

-48px 0px是搜索图标 ( icon-search) 的剪辑,但很容易显示任何其他图标。

如果有人需要:hover效果,背景必须再次以相同的形式键入。

于 2012-09-24T13:26:33.180 回答
4

我得到了这个工作,但有一些警告我还没有解决。

无论如何,这就是它的完成方式:

拿你的平均输入按钮:

<input type="submit" class="btn btn-success" value="Save">

从 glyphicons sprite 文件中剪下您想要的提交按钮的图标,确保它是 14x14 像素的图像。是的,在理想情况下,您可以重用精灵,如果有人知道这一点,我会很高兴听到它是如何完成的。:-)

一旦你这样做了,你可以为你的输入按钮编写 css,如下所示:

input[type='submit'] {
    background-image: url('../images/submit-icon.png'), #62C462; /* fallback color if gradients are not supported */
    background-image: url('../images/submit-icon.png'), -webkit-linear-gradient(top, #62C462, #51A351);
    background-image: url('../images/submit-icon.png'),    -moz-linear-gradient(top, #62C462, #51A351); /* For Fx 3.6 to Fx 15 */
    background-image: url('../images/submit-icon.png'),     -ms-linear-gradient(top, #62C462, #51A351); /* For IE 10 Platform Previews and Consumer Preview */
    background-image: url('../images/submit-icon.png'),      -o-linear-gradient(top, #62C462, #51A351); /* For Opera 11.1 to 12.0 */
    background-image: url('../images/submit-icon.png'),         linear-gradient(top, #62C462, #51A351); /* Standard syntax; must be the last statement */
    background-repeat: no-repeat;
    background-position: 5px 50%, 0cm 0cm;
    padding-left: 25px;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
}

input[type='submit']:hover {
    background-image: url('../images/submit-icon.png'), #51A351; /* fallback color if gradients are not supported */
    background-image: url('../images/submit-icon.png'), -webkit-linear-gradient(top, #51A351, #51A351);
    background-image: url('../images/submit-icon.png'),    -moz-linear-gradient(top, #51A351, #51A351); /* For Fx 3.6 to Fx 15 */
    background-image: url('../images/submit-icon.png'),     -ms-linear-gradient(top, #51A351, #51A351); /* For IE 10 Platform Previews and Consumer Preview */
    background-image: url('../images/submit-icon.png'),      -o-linear-gradient(top, #51A351, #51A351); /* For Opera 11.1 to 12.0 */
    background-image: url('../images/submit-icon.png'),         linear-gradient(top, #51A351, #51A351); /* Standard syntax; must be the last statement */
    background-position: 5px 50%, 0cm 0cm;
    padding-left: 25px;
}

适用于 Firefox 14、Chrome 21

在 IE 9 中不起作用

tl;dr:通过一点 css,您可以自动将图标放在提交按钮上,但您需要将图标放在单独的文件中,它在 Internet Explorer 中不起作用。

于 2012-08-27T22:43:18.593 回答
1

我认为它可以解决您的问题

<input type="text" placeholder="search here" />
<button type="submit"><i class="glyphicon glyphicon-search"></button>
于 2015-10-25T12:23:43.883 回答
1

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  
    <button type="button" class="btn btn-info">
      <span class="glyphicon glyphicon-search"></span> Search
    </button>
  </p>
 
    <a href="#" class="btn btn-success btn-lg">
      <span class="glyphicon glyphicon-print"></span> Print 
    </a>
  </p> 
</div>

</body>
</html>

于 2016-07-20T10:09:56.223 回答
0

将 input-append 与附加类一起使用

<div class="input-append">
  <input class="span2" id="appendedPrependedInput" type="text">
  <span class="add-on">.00</span>
</div>
于 2013-05-16T08:50:45.333 回答
-1

我想这是更好的方法,对我来说很好。

<form name="myform">
<!-- form fields -->
   <a href="#" class="btn btn-success" onclick="document.myform.submit();">
     Submit <i class="icon-plus"></i>&nbsp; Icon
   </a>
</form>
于 2013-01-28T10:13:22.047 回答