代码之家  ›  专栏  ›  技术社区  ›  leora Matt Lacey

如何禁用jqueryui对话框上的按钮?

  •  138
  • leora Matt Lacey  · 技术社区  · 14 年前

    on the jQuery UI dialog . 我似乎在上面链接的任何文档中都找不到这个。

    我在模式确认上有两个按钮(“确认”和“取消”)。在某些情况下,我想禁用“确认”按钮。

    14 回复  |  直到 14 年前
        1
  •  158
  •   Nick Craver    14 年前

    如果你把 .button() plugin/widget jqueryui所包含的(如果您拥有完整的库并且在1.8+上,那么您拥有它),您可以使用它来禁用按钮 直观地更新状态,如下所示:

    $(".ui-dialog-buttonpane button:contains('Confirm')").button("disable");
    

    You can give it a try here …或者,如果您使用的是旧版本或未使用button小部件,则可以如下方式禁用它:

    $(".ui-dialog-buttonpane button:contains('Confirm')").attr("disabled", true)
                                                  .addClass("ui-state-disabled");
    

    如果要将其放在特定的对话框中,例如按ID,请执行以下操作:

    $("#dialogID").next(".ui-dialog-buttonpane button:contains('Confirm')")
                  .attr("disabled", true);
    

    在其他情况下 :contains() 可能会出现误报然后你可以使用 .filter() 像这样,但是你知道你的两个按钮,这太过分了。

    $("#dialogID").next(".ui-dialog-buttonpane button").filter(function() {
      return $(this).text() == "Confirm";
    }).attr("disabled", true);
    

        2
  •  219
  •   Community CDub    8 年前

    看起来像任何人,即使是在 this linked question ,提出了这个解决方案,类似于Nick Craver给出的答案的第一部分:

    $("#dialog").dialog({
        width: 480,
        height: "auto",
        buttons: [
            {
                id: "button-cancel",
                text: "Cancel",
                click: function() {
                    $(this).dialog("close");
                }
            },
            {
                id: "button-ok",
                text: "Ok",
                click: function() {
                    $(this).dialog("close");
                }
            }
        ]
    });
    

    那么,在其他地方,你应该可以使用 API

    $("#button-ok").button("disable");
    
        3
  •  51
  •   KimvdLinde    9 年前

    您也可以使用 现已记录在案 disabled

    $("#element").dialog({
        buttons: [
        {
            text: "Confirm",
            disabled: true,
            id: "my-button-1"
        }, 
        {
            text: "Cancel",
            id: "my-button-2",
            click: function(){            
                   $(this).dialog("close");
            }  
    
        }]
    });
    

    要在对话框打开后启用,请使用:

    $("#my-button-1").attr('disabled', false);
    

    J中间: http://jsfiddle.net/xvt96e1p/4/

        4
  •  7
  •   Chris Pietschmann    14 年前

    $(function() {
        $("#dialog").dialog({
            height: 'auto', width: 700, modal: true,
            buttons: {
                'Add to request list': function(evt) {
    
                    // get DOM element for button
                    var buttonDomElement = evt.target;
                    // Disable the button
                    $(buttonDomElement).attr('disabled', true);
    
                    $('form').submit();
                },
                'Cancel': function() {
                    $(this).dialog('close');
                }
            }
        });
    }
    
        5
  •  2
  •   Sumeet_Pol    10 年前

    此代码禁用带有“您的按钮标签”的按钮。可以替换contains()中的name。 禁用

    $(".ui-dialog-buttonpane button:contains('YOUR_BUTTON_LABEL')").button("disable");
    

    将“您的按钮标签”替换为按钮标签。

    $(".ui-dialog-buttonpane button:contains('YOUR_BUTTON_LABEL')").button("enable");
    
        6
  •  1
  •   Chris Laplante    14 年前

    ui-button . 要禁用按钮:

    $("#myButton").addClass("ui-state-disabled").attr("disabled", true);
    

    除非动态创建对话框(这是可能的),否则您将知道按钮的位置。因此,要禁用第一个按钮:

    $("#myButton:eq(0)").addClass("ui-state-disabled").attr("disabled", true);
    

    这个 ui-state-disabled

        7
  •  1
  •   sergiodlopes    14 年前

    我创建了一个jQuery函数来简化这个任务。也许现在有更好的解决办法。。。不管怎样,这是我的20美分。:)

    只需将此添加到JS文件:

    $.fn.dialogButtons = function(name, state){
    var buttons = $(this).next('div').find('button');
    if(!name)return buttons;
    return buttons.each(function(){
        var text = $(this).text();
        if(text==name && state=='disabled') {$(this).attr('disabled',true).addClass('ui-state-disabled');return this;}
        if(text==name && state=='enabled') {$(this).attr('disabled',false).removeClass('ui-state-disabled');return this;}
        if(text==name){return this;}
        if(name=='disabled'){$(this).attr('disabled',true).addClass('ui-state-disabled');return buttons;}
        if(name=='enabled'){$(this).attr('disabled',false).removeClass('ui-state-disabled');return buttons;}
    });};
    

    禁用“dialog”类对话框上的“Ok”按钮:

    $('.dialog').dialogButtons('Ok', 'disabled');
    

    $('.dialog').dialogButtons('enabled');
    

    启用“关闭”按钮并更改颜色:

    $('.dialog').dialogButtons('Close', 'enabled').css('color','red');
    

    $('.dialog').dialogButtons().css('color','red');
    

        8
  •  1
  •   Ronny Sherer    13 年前
    function getDialogButton( jqUIdialog, button_names )
    {
        if (typeof button_names == 'string')
            button_names = [button_names];
        var buttons = jqUIdialog.parent().find('.ui-dialog-buttonpane button');
        for (var i = 0; i < buttons.length; i++)
        {
            var jButton = $( buttons[i] );
            for (var j = 0; j < button_names.length; j++)
                if ( jButton.text() == button_names[j] )
                    return jButton;
        }
    
        return null;
    }
    
    function enableDialogButton( jqUIdialog, button_names, enable )
    {
        var button = getDialogButton( jqUIdialog, button_names );
        if (button == null)
            alert('button not found: '+button_names);
        else
        {
            if (enable)
                button.removeAttr('disabled').removeClass( 'ui-state-disabled' );
            else
                button.attr('disabled', 'disabled').addClass( 'ui-state-disabled' );
        }
    }
    
        9
  •  1
  •   jfredys    13 年前

    您可以覆盖按钮数组,只留下所需的按钮。

    $( ".selector" ).dialog( "option", "buttons", [{
        text: "Close",
        click: function() { $(this).dialog("close"); }
    }] );
    
        10
  •  0
  •   Darin Dimitrov    14 年前

    您可以这样做来禁用第一个按钮,例如:

    $('.ui-dialog-buttonpane button:first').attr('disabled', 'disabled');
    
        11
  •  0
  •   Bhavin    11 年前

    Cancel: function(e) { $(e.target).attr( "disabled","disabled" ); }

    这是我找到的最短最简单的方法。

        12
  •  0
  •   crush    10 年前

    var dialog = $('#my-dialog').dialog({
        width: '100%',
        buttons: [
            { text: 'Submit', click: $.noop, 'data-bind': 'enable: items() && items().length > 0, click: onSubmitClicked' },
            { text: 'Enable Submit', click: $.noop, 'data-bind': 'click: onEnableSubmitClicked' }
        ]
    });
    
    function ViewModel(dialog) {
        var self = this;
    
        this.items = ko.observableArray([]);
    
        this.onSubmitClicked = function () {
            dialog.dialog('option', 'title', 'On Submit Clicked!');
        };
    
        this.onEnableSubmitClicked = function () {
            dialog.dialog('option', 'title', 'Submit Button Enabled!');
            self.items.push('TEST ITEM');
            dialog.text('Submit button is enabled.');
        };
    }
    
    var vm = new ViewModel(dialog);
    ko.applyBindings(vm, dialog.parent()[0]); //Don't forget to bind to the dialog parent, or the buttons won't get bound.
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
    
    <div id="my-dialog">
      Submit button is disabled at initialization.
    </div>

    魔法来自 jQuery UI source

    $( "<button></button>", props )
    

    基本上可以通过button对象调用任何jQuery实例函数。

    例如,如果要使用HTML:

    { html: '<span class="fa fa-user"></span>User' }
    

    { addClass: 'my-custom-button-class' }
    

    也许你疯了,你想在dom悬停时删除它:

    { mouseover: function () { $(this).remove(); } }
    

    我真的很惊讶,在无数这样的线程中似乎没有人提到这一点。。。

        13
  •  0
  •   Pramod Kumar    7 年前

    这对我有用--

    $("#dialog-confirm").html('Do you want to permanently delete this?');
    $( "#dialog-confirm" ).dialog({
        resizable: false,
        title:'Confirm',
        modal: true,
        buttons: {
            Cancel: function() {
                $( this ).dialog( "close" );
            },
            OK:function(){
                $('#loading').show();
                $.ajax({
                        type:'post',
                        url:'ajax.php',
                        cache:false,
                        data:{action:'do_something'},
                        async:true,
                        success:function(data){
                            var resp = JSON.parse(data);
                            $("#loading").hide();
                            $("#dialog-confirm").html(resp.msg);
                            $( "#dialog-confirm" ).dialog({
                                    resizable: false,
                                    title:'Confirm',
                                    modal: true,
                                    buttons: {
                                        Close: function() {
                                            $( this ).dialog( "close" );
                                        }
                                    }
                            });
                        }
                    });
            }
        }
    }); 
    
        14
  •  0
  •   Salman Arshad    6 年前

    构造对话框时可以禁用按钮:

    $(function() {
      $("#dialog").dialog({
        modal: true,
        buttons: [
          { text: "Confirm", click: function() { $(this).dialog("close"); }, disabled: true },
          { text: "Cancel", click: function() { $(this).dialog("close"); } }
        ]
      });
    });
    @import url("https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.min.css");
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
    
    <div id="dialog" title="Confirmation">
      <p>Proceed?</p>
    </div>

    也可以在创建对话框后随时禁用它:

    $(function() {
      $("#dialog").dialog({
        modal: true,
        buttons: [
          { text: "Confirm", click: function() { $(this).dialog("close"); }, "class": "confirm" },
          { text: "Cancel", click: function() { $(this).dialog("close"); } }
        ]
      });
      setTimeout(function() {
        $("#dialog").dialog("widget").find("button.confirm").button("disable");
      }, 2000);
    });
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
    
    <div id="dialog" title="Confirmation">
      <p>Button will disable after two seconds.</p>
    </div>