Вот несколько методов в JavaScript, которые предлагают пользователю ввести данные:
-
Использование функции
prompt():let userInput = prompt("Please enter your input:"); -
Создание пользовательской формы ввода HTML и доступ к ее значению с помощью JavaScript:
<input type="text" id="userInput"> <button onclick="getUserInput()">Submit</button> <script> function getUserInput() { let userInput = document.getElementById("userInput").value; // Use the userInput variable as needed } </script> -
Использование библиотеки или платформы, например пользовательского интерфейса jQuery:
$("#dialog").dialog({ modal: true, buttons: { Ok: function() { let userInput = $("#inputField").val(); // Use the userInput variable as needed $(this).dialog("close"); }, }, }); -
Использование библиотеки модальных диалогов, например SweetAlert:
Swal.fire({ title: "Enter your input:", input: "text", showCancelButton: true, confirmButtonText: "Submit", }).then((result) => { if (result.isConfirmed) { let userInput = result.value; // Use the userInput variable as needed } });