[英]Show Popover when keydown and keyup on input
如何復制用戶輸入的數字並通過javascript
或jquery
在Popover上顯示 ?
我的意思是,當用戶鍵入大於零的數字時,例如: 1000
,popover Show 1000
。
$(document).ready(function() { //this calculates values automatically getPriceAndPopUp(); $("#price").on("keydown keyup", function() { getPriceAndPopUp(); }); }); function getPriceAndPopUp() { var price = document.getElementById('price').value; if (!isNaN(price) && price > 0) { alert(price); } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <body> <form> Price:<br> <input type="number" name="price" id="price" class="form-control" min="0" required /> </form> </body>
您需要等待用戶完成鍵入后才能顯示該號碼,這需要使用超時功能延遲警報顯示
$(document).ready(function() { var timeout; //this calculates values automatically getPriceAndPopUp(); $("#price").on("keydown keyup", function() { clearTimeout(timeout); timeout = getPriceAndPopUp(); }); }); function getPriceAndPopUp() { var price = document.getElementById('price').value; return setTimeout(function() { if (!isNaN(price) && price > 0) { $('[data-content]').attr('data-content',price); $('[data-toggle="popover"]').popover('show'); } }, 400); }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> <body> <form> Price:<br> <input type="number" name="price" id="price" class="form-control" min="0" required data-toggle="popover" data-placement="bottom" data-content="0"/> </form> </body>
您可以在blur
事件上執行此操作。
$(document).ready(function() { //this calculates values automatically getPriceAndPopUp(); $("#price").on("blur", function() { getPriceAndPopUp(); }); }); function getPriceAndPopUp() { var price = document.getElementById('price').value; if (!isNaN(price) && price > 0) { alert(price); } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <body> <form> Price:<br> <input type="number" name="price" id="price" class="form-control" min="0" required /> </form> </body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.