jQuery UI dialog button text as a variable

后端 未结 12 1692
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 01:21

Can anyone tell me how can i use a variable for button text in jQuery UI dialog? I want to make a dynamic button name.

相关标签:
12条回答
  • 2020-12-13 02:08

    What you can do is assign the button in the dialog an ID and then manipulate it using standard jQuery.

    $("#dialog_box").dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        buttons: [{
            text: "Ok",
            "id": "btnOk",
            click: function () {
                //okCallback();
            },
    
        }, {
            text: "Cancel",
            click: function () {
                //cancelCallback();
            },
        }],
        close: function () {
            //do something
        }
    });
    

    Set button text:

     var newLabel = "Updated Label";
     $("#btnOk").html('<span class="ui-button-text">'+ newLabel +'</span>')
    
    0 讨论(0)
  • 2020-12-13 02:10
    1. The button option in jQuery UI dialog accepts objects and arrays.
    2. The buttons are instances of the button widget. Use the API instead of manipulating the buttons yourself.

    $(function() {
      // using textbox value instead of variable
      $("#dialog").dialog({
        width: 400,
        buttons: [
          { text: $("#buttonText0").val(), click: function() { $(this).dialog("close"); } }, 
          { text: $("#buttonText1").val(), click: function() { $(this).dialog("close"); } }
        ]
      });
      $("#updateButtonText").on("click", function() {
        var $buttons = $("#dialog").dialog("widget").find(".ui-dialog-buttonpane button");
        console.log($buttons.get());
    
        $buttons.eq(0).button("option", "label", $("#buttonText0").val());
        $buttons.eq(1).button("option", "label", $("#buttonText1").val());
    
        // few more things that you can do with button widget
        $buttons.eq(0).button("option", "icons", { primary: "ui-icon-check" });
        $buttons.eq(1).button("disable");
    
        $("#dialog").dialog("open");
      });
    });
    @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="Sample Dialog">
      <p>Proceed?</p>
    </div>
    
    <p style="text-align: center;">
      <input type="text" id="buttonText0" value="OK">
      <input type="text" id="buttonText1" value="Cancel">
      <input type="button" id="updateButtonText" value="Update Button Text">
    </p>

    0 讨论(0)
  • 2020-12-13 02:15

    Maybe I'm missing the point - but wouldn't the easiest way be to use the setter?

         $("#dialog_box").dialog({
            buttons: {
             [
                text:"OK",
                click: function() {
                    //... configure the button's function
                }
             ]
            });
    
            $("#dialog_box").dialog("option", "buttons", { "Close": function() { $(this).dialog("close"); } });
    
    0 讨论(0)
  • 2020-12-13 02:18

    This can be done in following steps :

    1. In JavaScript you can create Array of Buttons.
    2. Set buttons property to the Array of Buttons.

    Following Example explains above steps.

    1. Two buttons are defined in btnArray as follows;
     var btnArray = [
        { text: "Add Entry",
          click: function(){
            this.retVal = true;
            addRowIntoTemplateManifest();
            $(this).dialog('close');
          }
        },
        { text: "Cancel",
          click: function(){
            this.retVal = false;
            $(this).dialog('close');
          }
        }
      ];
    

    A custom function displayConfirmDialog_Dynamic() is written that accpets, Dialog header, Dialog Text, button Array. The call to this function is as follows:

    displayConfirmDialog_Dynamic("Add Template Manifest Entry?", "Do you want to add following Cuboid Entry to Template Manifest?\nCuboidNane: '" + json.cuboidName + "' CuboidId: " + json.cuboidId + "\non Server:"
    + json.serverUrl , btnArray );
    

    The function displayConfirmDialog_Dynamic is as follows:

    //Confirmation dialog Dynamic Buttons
    function displayConfirmDialog_Dynamic(dlgTitle, message, btnArray)
    {
        var retVal;
        $("div#dialog-confirm").find("p").html(message);
    
        var confirmDlg = $( "#dialog-confirm" ).dialog({
              resizable: false,
              height: "auto",
              width: 400,
              modal: true,
              title: dlgTitle,
              buttons: btnArray,
              show:{effect:'scale', duration: 700},
              hide:{effect:'explode', duration: 700}  
        });
    
        confirmDlg.dialog('option', 'buttons', btnArray);
        confirmDlg.prev(".ui-dialog-titlebar").css({"background":"#ffc000", "color":"#ffffff", "font-size":"13px", "font-weight":"normal"}) ;
        confirmDlg.dialog("open");  
    }
    

    The Confirm Dialog Template is defined as DIV tag as follows. Note that the title and contents of <p> tag are changed dynamically by JavaScript code.

    <div id="dialog-confirm" title="Empty the recycle bin?" style="display:none;">
      <p>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
    </div>
    

    The Screenshot of dialog box displayed by above code is shown below:

    0 讨论(0)
  • 2020-12-13 02:22

    The problem here is that the dialog plugin does not assign an id to its buttons, so it's quite difficult to modify them directly.

    Instead, initialise the dialog as normal, locate the button by the text it contains and add an id. The button can then be accessed directly to change the text, formatting, enable/disable it, etc.

    $("#dialog_box").dialog({
    buttons: {
        'ButtonA': function() {
            //... configure the button's function
        }
    });
    $('.ui-dialog-buttonpane button:contains(ButtonA)').attr("id","dialog_box_send-button");            
    $('#dialog_box_send-button').html('Send')       
    
    0 讨论(0)
  • 2020-12-13 02:22
    var buttonName = "something";
    $('#button-id').attr('value', buttonName);
    
    0 讨论(0)
提交回复
热议问题