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

javascript - how to show variables in sweetalert2 - Stack Overflow

programmeradmin6浏览0评论

I am trying to use sweetalert2 which seems to be good as easy to implement modals but it seems to be limited or I am just being thick. All I am trying to do is when it is activated I want it to show a javascript variable but it seems I cannot do this without JSON and loading all sorts? Is it me or am I being thick?

The var I want to show is this

            var ad1 = test5+' '+place.formatted_address;

and the way I am trying to show it is :

swal("details", "details 2"+ad1)  

})

Doing my head in, I have never used modals before either by the way but thanks for any help

I am trying to use sweetalert2 which seems to be good as easy to implement modals but it seems to be limited or I am just being thick. All I am trying to do is when it is activated I want it to show a javascript variable but it seems I cannot do this without JSON and loading all sorts? Is it me or am I being thick?

The var I want to show is this

            var ad1 = test5+' '+place.formatted_address;

and the way I am trying to show it is :

swal("details", "details 2"+ad1)  

})

Doing my head in, I have never used modals before either by the way but thanks for any help

Share Improve this question asked May 19, 2017 at 16:10 MiaMia 471 gold badge2 silver badges6 bronze badges 6
  • any console errors? – Mohit Bhardwaj Commented May 19, 2017 at 16:12
  • Nope? Even tried with the html output and tried to output PHP variables but no good??? Sort of getting the feeling its designed for alerts and warnings only but surely it should allow for custom information to be added? – Mia Commented May 19, 2017 at 16:19
  • yeah definitely, if it allows passing in string, then it should also allow variables in this manner because the end result it gets is still a string – Mohit Bhardwaj Commented May 19, 2017 at 16:20
  • Does it work fine if you alert a plain string e.g. swal("details", "details 2") ??? – Mohit Bhardwaj Commented May 19, 2017 at 16:21
  • yes, but the details and details are only text not variables – Mia Commented May 19, 2017 at 16:23
 |  Show 1 more ment

1 Answer 1

Reset to default 3

I added a variable here and it works just fine. Can you check if this is what you want:

var testVar = "TestVariable";

swal({
  title: "Error!",
  text: "Here's my " + testVar,
  type: "error",
  confirmButtonText: "Cool" 
 });
<script src="http://t4t5.github.io/sweetalert/dist/sweetalert-dev.js"></script> <link rel="stylesheet" type="text/css" href="http://t4t5.github.io/sweetalert/dist/sweetalert.css">

发布评论

评论列表(0)

  1. 暂无评论