【问题标题】:SweetAlert2 with HTML and SelectSweetAlert2 与 HTML 和 Select
【发布时间】:2017-04-06 19:55:51
【问题描述】:

我构建了一个包含 HTML 和选择功能的 sweetAlert2 pop。问题是选择框在 HTML 下的弹出窗口底部加载。

            var array = '[{"teachName" : "Pending Account", "teachNum" : "99999999"},{"teachName" : "test, test", "teachNum" : "101-temp"}]';
            var options = {};
            $.map(array,
            function(o){
                options[o.teachNum] = o.teachName;
            });
            swal({
                title: date.format("MM/DD/YYYY"),
                type: 'info',
                input: 'select',
                inputOptions: options ,
                html: 'Teacher: ' + $("#teacher").val() + '<br /><br /><div style="border:2px solid purple;" >' +
                    'Hours: ' +
                    '<input type="text" autofocus id="hours" style="display:block !important;" class="sweet-alert-input"/>' +
                    '<br />Notes: ' +
                    '<br /><input type="text" autofocus id="notes" style="display:block !important;" class="sweet-alert-input"/></div>' +
                    '<button type="' + btn + '" id="btnD" value="' + date.format("MM/DD/YYYY") + '" class="sweet-alert2-button">Save Record</button> ' +
                    '<button type="' + btn + '" id="btnC" style="background-color: #B9B9B9 !important;" class="sweet-alert2-button">Cancel</button>',

                showConfirmButton: false
            });

我希望选择框位于 HTML 代码上方。请告诉我如何解决这个问题。这是我的代码。

【问题讨论】:

    标签: jquery html sweetalert sweetalert2


    【解决方案1】:

    首先:你的数组定义是错误的:删除字符串分隔符。

    您可以使用sweetalert2 onOpen event 将您的选择框移到 HTML 代码上方

    为了实现这一点,您可以使用.insertBefore()

    onOpen: function(ele) {
       $(ele).find('.swal2-select').insertBefore($(ele).find('.swal2-content div'));
    }
    

    sn-p:

    var array = [{"teachName": "Pending Account", "teachNum": "99999999"}, {"teachName": "test, test", "teachNum": "101-temp"}];
    var options = {};
    $.map(array, function (o) {
        options[o.teachNum] = o.teachName;
    });
    var btn = 'button';
    swal({
        title: moment().format("MM/DD/YYYY"),
        type: 'info',
        input: 'select',
        inputOptions: options,
        html: 'Teacher: ' + $("#teacher").val() + '<br /><br /><div style="border:2px solid purple;" >' +
        'Hours: ' +
        '<input type="text" autofocus id="hours" style="display:block !important;" class="sweet-alert-input"/>' +
        '<br />Notes: ' +
        '<br /><input type="text" autofocus id="notes" style="display:block !important;" class="sweet-alert-input"/></div>' +
        '<button type="' + btn + '" id="btnD" value="' + moment().format("MM/DD/YYYY") + '" class="sweet-alert2-button">Save Record</button> ' +
        '<button type="' + btn + '" id="btnC" style="background-color: #B9B9B9 !important;" class="sweet-alert2-button">Cancel</button>',
    
        showConfirmButton: false,
        onOpen: function(ele) {
            $(ele).find('.swal2-select').insertBefore($(ele).find('.swal2-content div'));
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/sweetalert2/6.5.6/sweetalert2.min.css">
    <script src="https://cdn.jsdelivr.net/sweetalert2/6.5.6/sweetalert2.min.js"></script>

    【讨论】:

    • 太好了...感谢您的帮助。另外,是的,我知道数组是错误的。这只是在放入数组之前从 JSON 复制而来。
    • 完成。再次感谢。
    猜你喜欢
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 2019-12-10
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多