Để yêu cầu người dùng nhập dữ liệu từ trình duyệt, bạn cần sử dụng phương thức
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
1 do trình duyệt cung cấpPhương thức
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
1 cho phép bạn chấp nhận đầu vào của người dùng dưới dạng chuỗi và lưu trữ nó trên một biến như sauconst input = prompt[];
Phương pháp này cũng chấp nhận một
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
3 làm thông tin bổ sung để cho người dùng biết loại đầu vào mà ứng dụng của bạn đang mong đợiVí dụ: viết đoạn mã sau để hỏi tên người dùng
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
Hoặc viết gợi ý sau khi cần biết tuổi của người dùng
const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
1 sẽ được trình duyệt hiển thị như sauVí dụ nhắc trình duyệt
Khi đó phương thức
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
5 sẽ hiển thị kết quả như sauThông báo hiển thị kết quả
Bạn có thể tạo kiểu văn bản
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
3 dưới dạng câu hỏi hoặc gợi ý tùy theo yêu cầu của bạnNhận đầu vào của người dùng từ bảng điều khiển NodeJS
Để chấp nhận đầu vào của người dùng từ bảng điều khiển NodeJS, bạn cần sử dụng mô-đun
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
7 được cung cấpBạn có thể
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
8 mô-đun như sauconst readline = require["readline"];
Sau đó, bạn cần tạo một phiên bản
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
9 được kết nối với luồng đầu vào. Bạn tạo const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
9 bằng cách sử dụng phương thức const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
1, đồng thời chuyển các tùy chọn const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
2 và const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
3 làm đối số đối tượngVì bạn muốn đầu vào và đầu ra được ghi vào bàn điều khiển, bạn cần viết
const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
2 là const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
5 và const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
3 là const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
7Đây là một ví dụ về việc tạo giao diện đường đọc
const readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
Để yêu cầu đầu vào của người dùng, bạn cần gọi phương thức
const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
8 từ đối tượng const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
9, được gán cho biến const readline = require["readline"];
0 trong đoạn mã trênPhương thức
const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
8 nhận hai tham số
3 câu hỏi bạn muốn hỏi người dùng của mìnhconst input = prompt["What's your name?"]; alert[`Your name is ${input}`];
- Đối tượng
3 [tùy chọn] nơi bạn có thể truyền tín hiệuconst readline = require["readline"];
4const readline = require["readline"];
- Hàm
5 để thực thi khi nhận được câu trả lời, chuyểnconst readline = require["readline"];
6 cho hàmconst readline = require["readline"];
Bạn có thể bỏ qua đối tượng
const readline = require["readline"];
3 và chuyển hàm const readline = require["readline"];
5 làm tham số thứ haiĐây là cách bạn sử dụng phương pháp
const input = prompt["Please enter your age:"];
alert[`You are ${input} years old`];
8const readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
rl.question["What is your name? ", function [answer] {
console.log[`Oh, so your name is ${answer}`];
}];
Cuối cùng, bạn có thể đóng giao diện
const readline = require["readline"];
0 bằng cách gọi phương thức const readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
1 bên trong hàm gọi lạiconst readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
rl.question["What is your name? ", function [answer] {
console.log[`Oh, so your name is ${answer}`];
console.log["Closing the interface"];
rl.close[];
}];
Lưu tệp dưới dạng
const readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
2, sau đó gọi tập lệnh bằng NodeJS như thế này$ node ask.js
What is your name? Nathan
Oh, so your name is Nathan
Closing the interface
$
Và đó là cách bạn có thể yêu cầu đầu vào của người dùng bằng mô-đun NodeJS
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
7Bạn cũng có thể sử dụng
const readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
4 từ NodeJS để thêm bộ đếm thời gian cho câu hỏi của mình và hủy nó khi một khoảng thời gian nhất định đã trôi quaNhưng xin lưu ý rằng phương pháp
const readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
4 chỉ khả dụng cho NodeJS phiên bản 15 trở lên. Và thậm chí sau đó, phương pháp này vẫn còn thử nghiệmCâu hỏi sau sẽ bị hủy bỏ khi không có câu trả lời nào được đưa ra trong 10 giây sau lời nhắc. Mã này đã được thử nghiệm để hoạt động trên NodeJS phiên bản 16. 3. 0 trở lên
const readline = require["readline"];
const ac = new AbortController[];
const signal = ac.signal;
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
rl.question["What is your name? ", { signal }, [answer] => {
console.log[`Oh, so your name is ${answer}`];
console.log["Closing the console"];
process.exit[];
}];
signal.addEventListener[
"abort",
[] => {
console.log["The name question timed out!"];
},
{ once: true }
];
setTimeout[[] => {
ac.abort[];
process.exit[];
}, 10000]; // 10 seconds
Bạn có thể thêm bộ đếm thời gian như trong đoạn mã trên cho các câu hỏi nhạy cảm về thời gian
Như bạn có thể thấy từ ví dụ trên, mô-đun
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
7 khá phức tạp so với phương thức const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
1 dễ dàng từ trình duyệtNgoài ra, bạn có thể sử dụng mô-đun npm
const readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
8 để yêu cầu đầu vào của người dùng mà không cần sử dụng mô-đun const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
7Nhận đầu vào của người dùng từ NodeJS bằng mô-đun đồng bộ hóa nhắc nhở
Trước tiên, bạn cần cài đặt mô-đun
const readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
8 bằng npm hoặc Yarn như saunpm install prompt-sync
# or
yarn add prompt-sync
Sau đó, bạn chỉ cần
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
8 mô-đun const readline = require["readline"];
const rl = readline.createInterface[{
input: process.stdin,
output: process.stdout,
}];
8 và sử dụng phương thức const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
1 như trong trình duyệtHãy nhìn vào đoạn mã dưới đây
const input = prompt["What's your name?"];
alert[`Your name is ${input}`];
0Vì phương thức này là đồng bộ, phiên bản Node của bạn sẽ đợi đầu vào trước khi thực hiện dòng tiếp theo. Để biết thêm thông tin, bạn có thể truy cập vào