简体   繁体   中英

Unable get the value of textarea inside jquery ui dialog

I tried to put a textarea inside a dialog box and what I want is to get the value of that textarea if "ok" is click but I can't get the value. What might be the problem?

 $(document).on('click', '#open', function () { var txt = $('#txt').val(); $('#break-diag').dialog({ modal: true, title: 'Dialog', show: { effect: "scale", duration: 200 }, resizable: false, buttons: [{ text: "ok", click: function () { console.log(txt); } }] }); }); 
 #break-diag{ display:none; } 
 <link href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <button id="open">Open Dialog</button> <div id="break-diag"> <textarea id="txt"></textarea> </div> 

You are capturing the textarea value on click of open; which would be empty during this event.

The value should be captured on click of Ok

click: function () {
       var txt = $('#txt').val();
       console.log(txt);
}

You're trying to get the text when the dialogue opens. There's no text there, yet.

 $(document).on('click', '#open', function () { $('#break-diag').dialog({ modal: true, title: 'Dialog', show: { effect: "scale", duration: 200 }, resizable: false, buttons: [{ text: "ok", click: function () { var txt = $('#txt').val(); console.log(txt); } }] }); }); 
 #break-diag{ display:none; } 
 <link href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <button id="open">Open Dialog</button> <div id="break-diag"> <textarea id="txt"></textarea> </div> 

$(document).on('click', '#open', function () { 
    $('#break-diag').dialog({
        modal: true,
        title: 'Dialog',
        show: {
            effect: "scale",
            duration: 200
        },
        resizable: false,
        buttons: [{
            text: "ok",
            click: function () {
              var text=  $('#txt').val();
                alert(text);
            }
        }]
    });
});

You set the CSS style display:none with #break-diag DIV.

so how can you imagine that anything can be display.

so when you click on the button, display:none property should be removed from #break-diag DIV .

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