简体   繁体   English

从输入 jQuery 获取值

[英]Get value from input jQuery

I am trying to get the value from the first name input and pass it to the bootstrap alert when the user clicks the submit button, but can't manage to do that.我试图从名字输入中获取值,并在用户单击提交按钮时将其传递给引导程序警报,但无法做到这一点。

Here is my snippet:这是我的片段:

 $(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">&times;</a>Thank you for contacting us, </div>

I assume you wish to use the input value to complete the Bootstrap's alert text...我假设您希望使用input值来完成 Bootstrap 的警报文本...

You will need an element to place that value in I suggest a span .您将需要一个元素来将该值放入我建议的span Use the .val() method to get the input value... Then .text() to insert it in the 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">&times;</a>Thank you for contacting us, <span id="firstName"></span> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM