【问题标题】:jQuery on() event not firing for sweetalert2 textboxesjQuery on() 事件不会为 sweetalert2 文本框触发
【发布时间】:2023-03-22 13:55:01
【问题描述】:

我在 sweetalert2 中动态创建了文本框,如下所示:

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
});

还有 jQuery 函数来监听文本框的变化事件。

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

但在 sweetalert2 中更改文本框值时不会触发事件。 jQuery 已正确加载,它适用于 sweetalert2 模型之外的其他文本框。我也尝试在html: 上方的&lt;/table&gt; 之后添加&lt;script&gt;...&lt;/script&gt;,但仍然没有运气。有人可以帮帮我吗?任何意见将不胜感激。

【问题讨论】:

  • $('#name').on('change', function(e) {更改为$(document).on('change','#name', function(e) {
  • @guradio 它有效!感谢您的快速回复。你想在答案中发布这个以便我可以接受吗?
  • 我一定会发的

标签: javascript jquery sweetalert sweetalert2


【解决方案1】:

$('#name').on('change', function(e) { 更改为$(document).on('change','#name', function(e) {

  1. 正确委派活动

【讨论】:

    【解决方案2】:

    这是因为您正在使用

    $('#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.
    

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 2014-09-18
      相关资源
      最近更新 更多