附加bootstrap" list-group-item"到" list-group"用jquery

时间:2018-01-30 12:11:09

标签: jquery twitter-bootstrap

我有一个bootstrap" form-group"包含textarea和按钮表单控件,我想在按下按钮时将写在textarea内的文本追加到列表组

这是我的页面连接Jquery试用代码:



$(document).ready(function() {
  $('button:last').click(function() {
    comment = $('textarea').val();
    $('.list-group').append("<li class='list-group-item'>+comment+</li>");
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
<div class="container">
  <ul class="list-group">
  </ul>
</div>
<div class="container" style="margin-top: 60px">
  <form class="well col-xs-6 col-xs-offset-3">
    <div class="form-group">
      <label id="commentTitle" for="comment">Comment:</label>
      <textarea class="form-control" rows="5" id="comment"></textarea>
    </div>
    <button class="btn btn-primary ">submit</button>
  </form>
</div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

你需要改变

$('.list-group').append("<li class='list-group-item'>+comment+</li>");

进入$('.list-group').append("<li class='list-group-item'>"+comment+" </li>");(粗心的错误)  并添加type="button"

&#13;
&#13;
$(document).ready(function(){
            $('button:last').click(function() {
                comment = $('textarea').val();   
                $('.list-group').append("<li class='list-group-item'>"+comment+"</li>");
            });
        });
&#13;
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
    </head>
    <body>
    <div class= "container">
        <ul class="list-group">
        </ul>
        <div class="container" style="margin-top: 60px">
            <form class="well col-xs-6 col-xs-offset-3">
                <div class="form-group">
                    <label id="commentTitle" for="comment">Comment:</label>
                    <textarea   class="form-control" rows="5" id="comment"></textarea>
                </div>
                <button type="button" class="btn btn-primary ">submit</button>
            </form>
        </div> 
    </body>
    </html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您缺少双引号(&#34;)请检查以下代码。

&#13;
&#13;
$(document).ready(function() {
  $('button:last').click(function(event) {
    event.preventDefault();
    comment = $('textarea').val();
    $('textarea').val('');
    $('.list-group').append("<li class='list-group-item'>" + comment + "</li>");
  });
});
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
<div class="container">
  <ul class="list-group">
  </ul>
</div>
<div class="container" style="margin-top: 60px">
  <form class="well col-xs-6 col-xs-offset-3">
    <div class="form-group">
      <label id="commentTitle" for="comment">Comment:</label>
      <textarea class="form-control" rows="5" id="comment"></textarea>
    </div>
    <button class="btn btn-primary ">submit</button>
  </form>
</div>
&#13;
&#13;
&#13;

并查看此链接JSFiddle

答案 2 :(得分:0)

请在试用代码中尝试此操作;

comment = $('#comment').val();   
$('.list-group').append("<li class='list-group-item'>" + comment + "</li>");

答案 3 :(得分:0)

您可以使用以下代码:

$(document).ready(function() {

  $('#submit').click(function() {
    //alert($('#comment').val());
    $('#data').append('<li>' + $('#comment').val() + '</li>')

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
<div class="container">

  <div class="container">
    <ul class="list-group" id="data">
    </ul>
  </div>
  <div class="container" style="margin-top: 60px">
    <form class="well col-xs-6 col-xs-offset-3">
      <div class="form-group">
        <label id="commentTitle" for="comment">Comment:</label>
        <textarea class="form-control" rows="5" id="comment"></textarea>
      </div>
      <button type="button" id="submit" class="btn btn-primary ">submit</button>
    </form>
  </div>