duanmou9228
2012-04-30 12:44
浏览 51

如何从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");}}
        });

图片转代码服务由CSDN问答提供 功能建议

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

  $(“#dialog”)。dialog({\  n autoOpen:false,
 width:600,
 buttons:{
“Ok”:function(){
 $(this).dialog(“close”);}} 
}); 
 <  / code>  
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doupian9490 2012-04-30 12:51
    已采纳

    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");
                }}
        });
    
    已采纳该答案
    打赏 评论
  • dongzang7182 2012-04-30 12:46

    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.

    打赏 评论
  • dongliufa6380 2012-04-30 12:50

    Try this on "OK" funcion

    var myvalue= $("#yourinputtext").val();
    alert(myvalue);
    
    打赏 评论
  • duanhanzi8328 2012-04-30 12:53

    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");}
            }
        }
    );
    
    打赏 评论

相关推荐 更多相似问题