最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery on() event not firing for sweetalert2 textboxes - Stack Overflow

matteradmin5PV0评论

I have dynamically created textboxes in sweetalert2 like this:

swal({
    title: 'Enter Info',
    showCancelButton: true,
    html:   "<table>" +
                "<tr>" +
                    "<td>name</td>" +
                    "<td><input type='text' id='name'/></td>" +
                "</tr>"
                "<tr>" +
                    "<td>email</td>" +
                    "<td><input type='text' id='email'/></td>" +
                "</tr>"
            "</table>"
}).then(function(){
    // ajax
});

And jQuery function to listen textbox change event.

$(document).ready(function () { 
    <script type="text/javascript">
        $('#name').on('change', function(e) {
            console.log($(this).val());
        });
    </script>
});

But event is not being fired when changing textbox values inside sweetalert2. jQuery is properly loaded and it works on other textboxes outside of sweetalert2 model. I've also tried adding <script>...</script> after </table> in above html: but still no luck. Can somebody help me out please? Any input would be greatly appreciated.

I have dynamically created textboxes in sweetalert2 like this:

swal({
    title: 'Enter Info',
    showCancelButton: true,
    html:   "<table>" +
                "<tr>" +
                    "<td>name</td>" +
                    "<td><input type='text' id='name'/></td>" +
                "</tr>"
                "<tr>" +
                    "<td>email</td>" +
                    "<td><input type='text' id='email'/></td>" +
                "</tr>"
            "</table>"
}).then(function(){
    // ajax
});

And jQuery function to listen textbox change event.

$(document).ready(function () { 
    <script type="text/javascript">
        $('#name').on('change', function(e) {
            console.log($(this).val());
        });
    </script>
});

But event is not being fired when changing textbox values inside sweetalert2. jQuery is properly loaded and it works on other textboxes outside of sweetalert2 model. I've also tried adding <script>...</script> after </table> in above html: but still no luck. Can somebody help me out please? Any input would be greatly appreciated.

Share Improve this question edited Apr 21, 2017 at 9:52 Limon Monte 54.6k49 gold badges190 silver badges221 bronze badges asked Jul 28, 2016 at 6:52 Min Naing OoMin Naing Oo 1,1055 gold badges26 silver badges52 bronze badges 3
  • 2 change $('#name').on('change', function(e) { to $(document).on('change','#name', function(e) { – guradio Commented Jul 28, 2016 at 6:53
  • @guradio it works! Thanks for very fast reply. Would u like to post this in the answer so I that I can accept? – Min Naing Oo Commented Jul 28, 2016 at 6:57
  • sure i will post it – guradio Commented Jul 28, 2016 at 6:59
Add a ment  | 

2 Answers 2

Reset to default 6

change $('#name').on('change', function(e) { to $(document).on('change','#name', function(e) {

  1. Delegate the event properly

this happens because you are using

$('#name').on('change', function(e) {});  // this works for static dom

$(document).on('change','#name', function(e) {});  // this works for static as well as content dynamically added in dom.
Post a comment

comment list (0)

  1. No comments so far