我试图从输入的名字中获取值,并在用户单击submit按钮时将其传递给引导警报,但无法做到这一点。
下面是我的代码片段:
$(document).ready(function() {
$("#submit-button").click(function() {
$("#myAlert").show("fade"), $("#fname").attr("value");
event.preventDefault();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<div id="myAlert" class="alert alert-success collapse">
<a href="#" class="close" data-dismiss="alert">×</a>Thank you for contacting us,
</div>
发布于 2019-06-20 07:58:49
我假设您希望使用input
值来完成引导程序的警告文本...
您需要一个元素来将该值放入我建议的span
中。使用.val()
方法获取input
值...然后使用.text()
将其插入到span
中。
$(document).ready(function() {
$("#submit-button").click(function() {
$("#firstName").text($("#fname").val()); // Use the input's value for the span's text
$("#myAlert").show("fade"); // Show the Bootstrap's alert
event.preventDefault();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
Enter your first name: <input id="fname"><br>
<button id="submit-button">Submit</button>
<div id="myAlert" class="alert alert-success collapse">
<a href="#" class="close" data-dismiss="alert">×</a>Thank you for contacting us, <span id="firstName"></span>
</div>
https://stackoverflow.com/questions/56676933
复制相似问题