最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add HTML input into sweet alert? - Stack Overflow

programmeradmin3浏览0评论

I am trying to add radio button into dialog box using sweet alert but I'm not able to do it. Following is the code:

swal({
        title: "<small>Please select an reason to cancel this job !</small>",
        type: "warning",
        text:"<input type=\"radio\" name=\"reason\" value=\"male\">Reason 1<br><input type=\"radio\" name=\"reason\" value=\"female\">Reason 2<br><input type=\"radio\" name=\"reason\" value=\"female\">Other Reason",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes",
        cancelButtonText: "No",
        closeOnConfirm: false,
        closeOnCancel: false,
        html: true
    },
            function(isConfirm){
                if (isConfirm) {
                    swal("Result !","Job cancelled successfully.");
                } else {
                    swal("Cancelled  !", "Process aborted");
                }
            });

I am trying to add radio button into dialog box using sweet alert but I'm not able to do it. Following is the code:

swal({
        title: "<small>Please select an reason to cancel this job !</small>",
        type: "warning",
        text:"<input type=\"radio\" name=\"reason\" value=\"male\">Reason 1<br><input type=\"radio\" name=\"reason\" value=\"female\">Reason 2<br><input type=\"radio\" name=\"reason\" value=\"female\">Other Reason",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes",
        cancelButtonText: "No",
        closeOnConfirm: false,
        closeOnCancel: false,
        html: true
    },
            function(isConfirm){
                if (isConfirm) {
                    swal("Result !","Job cancelled successfully.");
                } else {
                    swal("Cancelled  !", "Process aborted");
                }
            });
Share Improve this question edited Feb 23, 2016 at 17:07 Michał Perłakowski 92.6k30 gold badges163 silver badges186 bronze badges asked Aug 14, 2015 at 14:50 b22b22 3036 gold badges10 silver badges23 bronze badges 1
  • What error are you receiving? More information is helpful when posting questions. See the How to ask page for help in improving your questions. – Madness Commented Aug 14, 2015 at 15:17
Add a ment  | 

2 Answers 2

Reset to default 5

SweetAlert2 supports radio inputs out of the box: https://sweetalert2.github.io/#input-radio

Swal.fire({
  title: 'Select color',
  input: 'radio',
  inputOptions: {
    '#ff0000': 'Red',
    '#00ff00': 'Green',
    '#0000ff': 'Blue'
  },

  // validator is optional
  inputValidator: function(result) {
    if (!result) {
      return 'You need to select something!';
    }
  }
}).then(function(result) {
  if (result.isConfirmed) {
    Swal.fire({
      icon: 'success',
      html: 'You selected: ' + result.value
    });
  }
})
<script src="https://cdn.jsdelivr/npm/sweetalert2@11"></script>

Default sweetalert's stylesheet hides all input fields in alerts, so you have to restore initial values:

.sweet-alert input {
   display: initial;
   width: auto;
   height: auto;
   margin: auto;
}
发布评论

评论列表(0)

  1. 暂无评论