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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘