简体   繁体   中英

how to get the user input from prompt

I have this code (in jQuery Impormptu):

var statesdemo = {
    state0: {
        title: 'Name',
        html:'<label>name <input type="text" name="name" value=""></label><br />',
        buttons: { Next: 1 },
        submit:function(e,v,m,f){ 
            console.log(f);

            e.preventDefault();
            $.prompt.goToState('state1');
        }
    },  
    state1: {
        title: 'choice',
        html:'<label>choice: <select name="travel" multiple>'+
        '<option value="1" selected>1</option>'+
        '<option value="2">2</option>'+
        '</select></label>',
        buttons: { Back: -1, Done: 1 },
        focus: 1,
        submit:function(e,v,m,f){ 
            console.log(f);

            e.preventDefault();
            if(v==1) {$.prompt.close();}
            if(v==-1) $.prompt.goToState('state0');
        }
    }
};

and with simple jquery I have this snippet:

$('#id').bind("dblclick", function() {
  console.log('target id: ' + event.target.id);
  f = $.prompt(statesdemo);
});

how can I get the user input when the prompt is finished? the $.prompt does not return anything. Also, the problem is that the $.prompt is a non blocking function. so the programs continues without waiting the user input.

try something like this

            $('#my_a').click(function() {
                f = $.prompt(statesdemo);
                if(f.name!==undefined){
                      console.log(f.name);
                }
                if(f.travel!==undefined){
                      console.log(f.travel);
                }
            });

I've created a demo with an extra button that logs user input .

You can achieve this by creating an object to contain user input within Impromptu objects.

var userInput = {};

Next, create a function that takes a form input change event, and assigns the input's current value to a property in the object we just created with the same name as the input.

function registerUserInput( changeEvent ){
    var input = changeEvent.target;

    userInput[ input.name ] = $( input ).val();
};

Then we create an event listener on the body for all change events that occur within an Impromptu wrapper ( .jquibox )

$( 'body' ).on( 'change', '.jqibox *', registerUserInput );

From this point onwards if ever an input within an Impromptu changes, its value will be recorded in the userInput object. So, to get the name input in your example:

userInput.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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