How to take in text input from a keyboard and store it into a variable?

前端 未结 7 2049
清酒与你
清酒与你 2020-12-30 05:43

I would just like something simple to read text from a keyboard and store it into a variable. So for:

var color = \'blue\'

I would like the

相关标签:
7条回答
  • 2020-12-30 05:56

    There are three solution for it on NodeJS platform

    1. For asynchronous use case need, use Node API: readline

    Like: ( https://nodejs.org/api/readline.html )

    const readline = require('readline');
    
    const rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout
    });
    
    rl.question('What do you think of Node.js? ', (answer) => {
      // TODO: Log the answer in a database
      console.log(`Thank you for your valuable feedback: ${answer}`);
    
      rl.close();
    });
    
    1. For synchronous use case need, use NPM Package: readline-sync Like: ( https://www.npmjs.com/package/readline-sync )

      var readlineSync = require('readline-sync');

      // Wait for user's response. var userName = readlineSync.question('May I have your name? '); console.log('Hi ' + userName + '!');

    2. For all general use case need, use **NPM Package: global package: process: ** Like: ( https://nodejs.org/api/process.html )

    For taking input as argv:

    // print process.argv
    process.argv.forEach((val, index) => 
    {
      console.log(`${index}: ${val}`);
    });
    
    0 讨论(0)
  • 2020-12-30 06:03

    Node has a built in API for this...

    const readline = require('readline');
    
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    
    rl.question('Please enter a color? ', (value) => {
        let color = value
        console.log(`You entered ${color}`);
        rl.close();
    });
    
    0 讨论(0)
  • 2020-12-30 06:09

    You can use stdio for this. It is as simple as follows:

    import { ask } from 'stdio';
    const color = await ask('What is your keyboard color?');
    

    This module includes retries if you decide to accept only some predefined answers:

    import { ask } from 'stdio';
    const color = await ask('What is your keyboard color?', { options: ['red', 'blue', 'orange'], maxRetries: 3 });
    

    Take a look at stdio, it includes other features that may be useful for you (like command-line arguments parsing, standard input reading at once or by lines...).

    0 讨论(0)
  • 2020-12-30 06:10

    You can use the module 'readline' for this: http://nodejs.org/api/readline.html - the first example in the manual demonstrates how to do what you asked for.

    0 讨论(0)
  • 2020-12-30 06:16

    I would suggest the readline-sync module as well if you don't require something asynchronous.

    # npm install readline-sync
    
    const readline = require('readline-sync');
    
    let name = readline.question("What is your name?");
    
    console.log("Hi " + name + ", nice to meet you.");
    
    0 讨论(0)
  • 2020-12-30 06:16

    If I understood your need, that should do it:

    html:

    <input id="userInput" onChange="setValue()" onBlur="setValue()">
    

    javascript:

    function setValue(){
       color=document.getElementById("userInput").value;
       //do something with color
    }
    

    if you don't need to do something everytime the input changes, you can just get the input whenever you want to do something with 'color':

    html:

    <input id="userInput">
    

    javascript:

    color=document.getElementById("userInput").value;
    
    0 讨论(0)
提交回复
热议问题