custom choices in javascript confirm dialog

久未见 提交于 2019-11-27 02:08:11

In short, you can't.

You might want to consider looking into using something like a jQuery UI dialog instead.

You could ask the user for an answer using:

var userChoice = prompt("Question");

You could loop that sentence until the user enters an answer within the valid ones.

You can't. Use some javascript UI (jQuery UI, YUI, Mootools) library and mimic a dialog you need.

// custom Confirm builder
function OnConfirm(text, func) {

    var _confirm = $('<div/>').addClass('confirm');
    _confirm.append($('<h2/>').text(text));

    _confirm.append('<br/><br/>');

    var _btnCancel = $('<input/>').attr('type', 'button').val('cancel')
        .bind('click', function () {
            $(this).parent('.confirm').hide();
            func(false);
        });

    var _btnApply = $('<input/>').attr('type', 'button').val('OK')
        .bind('click', function () {
            $(this).parent('.confirm').hide();
            func(true);
        });

    _confirm.append(_btnCancel);
    _confirm.append(_btnApply);
    $('body').append(_confirm);
}

$(function () { // documen.loaded
    $('#testLink').click(function (e) {
        e.preventDefault(); ;
        var _href = $(this).attr('href');
        var _title = $(this).attr('title');
        // call custom confirm function with callback function
        OnConfirm(_title, function (_isContinue) {               
                if (_isContinue) {
                    location.href = _href;
                }
            }
        );
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!