Dynamically bind error message along with a icon to a single jQuery dialog

廉价感情. 提交于 2020-02-06 08:03:10

问题


I have created a global jQuery dialog to display all error messages in the application. I am able to bind the error message to the dialog, however I am unable to show the icon along with. For sake of simplicity I have provided a sample with a generic google image.

Any leads will be appreciated or if there's a better way to do it please do mention. Thanks in advance

function showAlertDialog(message) {
            var $dialog = $('#ErrorMessageDialog')
                   .html(message)
                   .dialog({
                       modal: true,
                       title: 'Data Error!',
                       width: 400,
                       buttons: {
                           Ok: function () {
                               $(this).dialog('close');
                           }
                       }
                       
                   });
            $dialog.dialog('open');
        }
        
        $("#btnOk").click(function(){
        showAlertDialog('Ok is clicked');
        });
        
        $("#btnCancel").click(function(){
        showAlertDialog('Cancel is clicked');
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>

<div id="ErrorMessageDialog" style="display:none;">
        <table>
            <tr>
                <td><img src="https://www.google.co.in/logos/doodles/2014/world-cup-2014-1-6584893165273088-res.png"/></td>/*display icon*/
                <td></td>/*display error message*/
            </tr>
        </table>
    </div>

<input type="button" id="btnOk" value="OK"/>
<input type="button" id="btnCancel" value="Cancel"/>

回答1:


You must select the specific table cell you want to update. Consider using Class selector.

Example

$(function() {
  function showAlertDialog(ttl, msg) {
    var $dialog = $('#ErrorMessageDialog')
      .dialog({
        modal: true,
        title: ttl,
        width: 400,
        buttons: {
          Ok: function() {
            $(this).dialog('close').dialog("destroy");
          }
        }
      });
    $('.message', $dialog).html(msg);
    $dialog.dialog('open');
  }

  $("#btnOk").click(function() {
    showAlertDialog('Alert!', 'Ok is clicked');
  });

  $("#btnCancel").click(function() {
    showAlertDialog('Alert!', 'Cancel is clicked');
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>

<input type="button" id="btnOk" value="OK" />
<input type="button" id="btnCancel" value="Cancel" />

<div id="ErrorMessageDialog" style="display:none;">
  <table>
    <tr>
      <td class="icon"><img src="https://www.google.co.in/logos/doodles/2014/world-cup-2014-1-6584893165273088-res.png" /></td>
      <td class="message"></td>
    </tr>
  </table>
</div>

Hope that helps.




回答2:


That is because you are replacing the content of #ErrorMessageDialog with only message. You should target the required td element not the whole #ErrorMessageDialog when associating the message. Sample code will be like:

function showAlertDialog(message) {
            var $dialog = $('#ErrorMessageDialog .target')
                   .html(message)
                   .dialog({
                       modal: true,
                       title: 'Data Error!',
                       width: 400,
                       buttons: {
                           Ok: function () {
                               $(this).dialog('close');
                           }
                       }

                   });
            $dialog.dialog('open');
        }

        $("#btnOk").click(function(){
        showAlertDialog('Ok is clicked');
        });

        $("#btnCancel").click(function(){
        showAlertDialog('Cancel is clicked');
        });

==

<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>

<div id="ErrorMessageDialog" style="display:none;">
        <table>
            <tr>
                <td><img src="https://www.google.co.in/logos/doodles/2014/world-cup-2014-1-6584893165273088-res.png"/></td>/*display icon*/
                <td class="target"></td>/*display error message*/
            </tr>
        </table>
    </div>

<input type="button" id="btnOk" value="OK"/>
<input type="button" id="btnCancel" value="Cancel"/>

Here I am targeting .target class which I have given to the target td element where you want to associate the message.




回答3:


function showAlertDialog(message) {
var img='<img src="https://www.google.co.in/logos/doodles/2014/world-cup-2014-1-6584893165273088-res.png"/>';
            var $dialog = $('#ErrorMessageDialog')
                   .html(message+img)
                   .dialog({
                       modal: true,
                       title: 'Data Error!',
                       width: 400,
                       buttons: {
                           Ok: function () {
                               $(this).dialog('close');
                           }
                       }
                       
                   });
            $dialog.dialog('open');
        }
        
        $("#btnOk").click(function(){
        showAlertDialog('Ok is clicked');
        });
        
        $("#btnCancel").click(function(){
        showAlertDialog('Cancel is clicked');
        });
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>

<div id="ErrorMessageDialog" style="display:none;">
        <table>
            <tr>
                <td><img src="https://www.google.co.in/logos/doodles/2014/world-cup-2014-1-6584893165273088-res.png"/></td>
                <td></td>
            </tr>
        </table>
    </div>

<input type="button" id="btnOk" value="OK"/>
<input type="button" id="btnCancel" value="Cancel"/>

append the image along with dialog message.



来源:https://stackoverflow.com/questions/53596132/dynamically-bind-error-message-along-with-a-icon-to-a-single-jquery-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!