[英]Get value from input jQuery
我試圖從名字輸入中獲取值,並在用戶單擊提交按鈕時將其傳遞給引導程序警報,但無法做到這一點。
這是我的片段:
$(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>
我假設您希望使用input
值來完成 Bootstrap 的警報文本...
您將需要一個元素來將該值放入我建議的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.