doujie9252 2018-09-18 07:53
浏览 62
已采纳

如何使用jquery获取警报上的复选框值? [重复]

This question already has an answer here:

<script>
    $(document).ready(function(){
        $("#city").change(function(){
            name = $(this).val();
            $.ajax({
               type:"GET",
               dataType: "json",
               data:{name: name},
               dataType: 'json',
               contentType: "application/json; charset=utf-8",
               url:"test.php",
               async:false,
               success:function(data)
               {
                    $.each(data.PostOffice, function(key, item) {

                        $("#pincode").append("<li><input type='checkbox' id='"+item.PINCode+"' name='pin' class='pin'/>"+item.Name+"</li>");
                    });
               }
            });
        });

        $(".pin").on('click',function(){
            pin = this.id;
            alert(pincode);
        });
    });
</script>

In this code I am using append function inside the each loop as you can see above the code. Now, What happen I have multiple checkbox when I change city but when I use $(".pin").on('click',function(){ to get the id of checkbox it doesn't work. So, How can I get id onclick class="pin"? Please help me.

Thank You

</div>
  • 写回答

1条回答 默认 最新

  • doumi9661 2018-09-18 07:56
    关注

    Since you are adding the HTML for checkbox inside AJAX call dynamically after the page has been loaded, the function $(".pin").on('click',function(){}) will not work as it will not find element with .pin class on page load. So, you need to assign a click event from the document reference as:

    $(document).on('click','.pin', function(){
        pin = this.id;
        alert(pin);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭