简体   繁体   English

获取ID和值动态创建的元素jquery

[英]get id and value dynamically created element jquery

I need the id(and other attributes such as value) of a span i previously created on an ajax event. 我需要先前在ajax事件上创建的范围的id(以及其他属性,例如value)。 Is there a way to do this? 有没有办法做到这一点?

This is how the span is created on php post: 这是在php post上创建跨度的方法:

    echo "<span class='non-skin-symptom-choice disease_option' ".
        "onclick='showinfo(".$var[0].");' id=".$var[0].">"
        .$var[1]." -- ".number_format($var[3]*100, 2, '.', '')."%</span>";

and I want to get its id whenever a checkbox is clicked. 并且我想在单击复选框时获取其ID。

    $(".chb").click(function(){

        var id= $(this).attr('id');
        var list=[];

        $('.disease_option').each(function (){
            alert("this.val=="+ $(this).attr("val"));        //i need the value here
            var str= $(this).attr("value").split(" -- ")[1];
            alert(str);
            str=str.slice(0,str.length - 1);
            if(parseFloat(str) >=support)
                list.push(id)                                //i need the id here
        });

the checkbox is not dynamically created, so $(".chb").click(function(){} works. 该复选框不是动态创建的,因此$(“。chb”)。click(function(){})有效。

somehow, $(this).attr("id") works but $(this).attr("val") returns undefined... i also tried $(this).attr("value") but same results. 不知何故,$(this).attr(“ id”)可以工作,但是$(this).attr(“ val”)返回未定义...我也尝试过$(this).attr(“ value”)但结果相同。 $(this).val returns empty. $(this).val返回空。

use 采用

$(document).on('click','.chb',function(){
  var id = $(".non-skin-symptom-choice").attr("id"); 
})

as this have a high level event attachment and it can get the elements who have been created on a runtime 因为它具有高级事件附件,它可以获取在运行时创建的元素

尝试这个

alert($(".non-skin-symptom-choice").attr("id"));

The click() binding you're using is called a "direct" binding which will only attach the handler to elements that already exist. 您正在使用的click()绑定称为“直接”绑定,它将仅将处理程序附加到已经存在的元素上。 It won't get bound to elements created in the future. 它不会绑定到将来创建的元素。 To do that, you'll have create a "delegated" binding by using on() 为此,您将使用on()创建“委托”绑定

$(document).on('click','.chb',function(){
  var id = $(".non-skin-symptom-choice").attr("id"); 
})

possible duplicate: Click event doesn't work on dynamically generated elements 可能重复: Click事件不适用于动态生成的元素

If your DOM node did not exist when the page loaded (ie. it was added to the page via AJAX after the page loaded), jQuery cannot see it if you try to update it or read it with jQuery methods. 如果页面加载时您的DOM节点不存在(即,在页面加载后通过AJAX将其添加到页面中),则如果您尝试更新它或使用jQuery方法读取它,jQuery将无法看到它。 One way to overcome this is to set up your jQuery function to reference the class or ID of the parent of the node you want to target (assuming the parent class is loaded into the page on page load), along with the class of the node you want to target. 解决此问题的一种方法是设置jQuery函数以引用要定位的节点的父级的类或ID(假设父类在页面加载时加载到页面中)您想要定位。 For example: 例如:

$(".chb").click(function(){
    var id = $(".your-parent-class .non-skin-symptom-choice").attr("id");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM