如何从jQuery UI对话框返回用户输入

如何从jQuery UI对话框返回用户输入

问题描述:

I want to open a jQuery UI dialog box and the box will have an input field which will take username. I want that when user presses the OK key on the dialog box, the dialog should return the username back to jQuery somehow

$("#dialog").dialog({
            autoOpen: false,
            width: 600,
            buttons: {
                "Ok": function() {
                $(this).dialog("close");}}
        });

我想打开一个jQuery UI对话框,该框将有一个输入字段,该字段将带有用户名。 我希望当用户按下对话框上的OK键时,对话框应该以某种方式将用户名返回给jQuery p>

  $(“#dialog”)。dialog({\  n autoOpen:false,
 width:600,
 buttons:{
“Ok”:function(){
 $(this).dialog(“close”);}} 
}); 
   pre> 
  div>

A dialog is asynchronous, which makes it meaningless to "return" something.
You can, however, use one of the callback functions to perform actions with the user input.
For example, using the 'OK' button callback:

$("#dialog")
    .append(
        $("<input>")
            .attr({
                "type" : "text",
                "id" : "user-input"
            })
    )
    .dialog({
        autoOpen: false,
        width: 600,
        buttons: {
            "Ok": function() {
                alert( $("#user-input").val() );
                $(this).dialog("close");
            }}
    });

If you just have a div on your page with ID dialog that contains the input field, just select it using a regular jQuery selector when the user presses OK.

Try this on "OK" funcion

var myvalue= $("#yourinputtext").val();
alert(myvalue);

You can capture the value in the ok button click handler, and then do with it as your require:

$("#dialog").dialog({
    autoOpen: false,
    width: 600,
    buttons: {
        "Ok": function() {
            var inputValue = $("#myInput").val();
            // do stuff with the value...

            $(this).dialog("close");}
        }
    }
);