gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:32:33 24 4
gpt4 key购买 nike

我有一个包含文本区域和按钮表单控件的 Bootstrap “表单组”,我想在按下按钮时将文本区域内写入的文本附加到列表组

这是我的页面,其中包含 Jquery 试用代码:

$(document).ready(function() {
$('button:last').click(function() {
comment = $('textarea').val();
$('.list-group').append("<li class='list-group-item'>+comment+</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">
<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>

最佳答案

你需要改变

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

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

$(document).ready(function(){
$('button:last').click(function() {
comment = $('textarea').val();
$('.list-group').append("<li class='list-group-item'>"+comment+"</li>");
});
});
<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>

关于jquery - 使用 jquery 将 bootstrap "list-group-item"附加到 "list-group",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48521180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com