从 bootbox 中的 textarea 获取值

Get value from textarea in bootbox

我有一个带有文本区域的 bootbox.dialog。我想将带有 rowbrakes 的 textarea 的值保存在写入数据库的同一位置。所以我需要用 /n<br> 创建一个字符串。

现在我的值未定义。

这是我的代码:

$('#btnComment').click(function () {        
    var comment = $("#lblComment1").html();
    popup.dialog({
        title: translator.get('EditComment'),

        message: "<textarea cols='60' rows='6' name='editComment'>" + comment + "</textarea>",

        buttons: {
            confirm: {
                label: translator.get('EditComment'),
                className: "btn-success",

                callback: function (result) {
                    if (result === null) {

                    } else {
                        $("#editComment").val(result);
                        $("#lblComment1").html(result);                            

                        var ajaxData = {
                            Type: "Comment",
                            OrderId: $("#lblOrder").html(),
                            newValue: $("#editComment").val(),
                            MiddocID: $("#hidMiddocID").val()
                        }

                        $.ajax({
                            type: 'post',
                            url: configuration.baseUrl + '/api/OrdersPostback', //
                            dataType: "json",
                            data: JSON.stringify(ajaxData),
                            contentType: "application/json; charset=utf-8"
                        }).then(function (bResult) {
                            if (bResult) {
                                $("#editComment").val(result);
                                $("#lblComment1").html(result);
                            }
                        });
                        //$("#hidBtnComment").click();
                    }
                }
            },

            cancel: {
                label: translator.get('Cancel'),
                className: "btn-default"
            }
        },


    })

});

对我可以做什么有什么建议吗?

把你的<textarea>改成这样:

message: "<textarea id='editComment' cols='60' rows='6' name='editComment'>" + comment + "</textarea>",

请注意,我添加了您最初没有添加的 id="editComment"。现在该元素将可以通过 $('#editComment') 获取,您可以使用 .val() 和其他 jQuery methods/plugins.

var dialog = bootbox.prompt({
    title: "This is a prompt with a textarea!",
    inputType: 'textarea',
    callback: function (result) {
        if (result ) {
            console.log (dialog.find('textarea.bootbox-input-textarea').val());
        }
    }
});

可以通过textarea获取值class "bootbox-input-textarea" http://bootboxjs.com/documentation.html#prompt-option-input-type