doujie9252 2018-09-18 07:53 采纳率: 0%
浏览 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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同