带按钮的Bootstrap输入组textarea

时间:2014-07-03 15:30:39

标签: html twitter-bootstrap

我正在尝试实现一个简单的提交输入组。我目前有以下内容:

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div class="input-group">
    <textarea class="form-control custom-control" rows="3" style="resize:none"></textarea> <span class="input-group-btn">
                                    <button class="btn btn-primary">                            
                                        <span>Send</span>

    </button>
    </span>
</div>

我希望“发送”按钮占据文本区域的整个高度。这可行吗?

3 个答案:

答案 0 :(得分:83)

您需要做的是使用input-group-addon实用程序。这将允许按钮的空间占据整个高度。您需要调整填充,因为它仅适用于悬停(颜色更改)。我建议复制所有相关的类样式并自己创建(对于颜色和背景颜色,因此它不是默认的Bootstrap样式)。

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div class="input-group">
    <textarea class="form-control custom-control" rows="3" style="resize:none"></textarea>     
    <span class="input-group-addon btn btn-primary">Send</span>
</div>

我知道它不符合Bootstrap文档,但是他们的文档假设您没有使用textarea。

答案 1 :(得分:2)

只是详细说明Aibrean的答案,并进一步说明如何使他/她的span标签可操作。

我们需要将span绑定到事件监听器,在下面的示例中我使用jQuery。

&#13;
&#13;
; (function ($) {
  $('#submitMyForm').on('click', function () {
    //$('#myForm').submit(); // js fiddle won't allow this
    alert("form submitted");
  });
  
})(jQuery)
&#13;
#exampleWrapper {
  padding: 100px;
}
&#13;
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div id="exampleWrapper">
  <form id="myForm" method="POST" action="?process-form">
      <div class="input-group">
          <textarea class="form-control custom-control" rows="3" style="resize:none"></textarea>
          <span class="input-group-addon btn btn-primary" id="submitMyForm">Send</span>
      </div>
  </form>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

让我们假设我们要创建一个文本区域,在左侧(开始)带有一个GO按钮,在右侧(结束)带有搜索符号。 这是使用引导程序执行的代码

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
Creating a button in text area using bootstrap        
</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style>

</style>
</head>
<body>
<div class="container-fluid">


<label for="search" class="sr-only">Search</label>
<input type="text" id="search" class="form-control" placeholder="Search">
<span class="glyphicon glyphicon-search form-control-feedback"></span>


</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
</script>
<script src="js/bootstrap.min.js">
</script>
</body>
</html>