简体   繁体   English

jQuery生成的DOM元素事件处理程序挑战

[英]jquery-generated DOM elements event handler challenge

I am dynamically building a page using jquery/ajax/json: 我正在使用jquery / ajax / json动态构建页面:

 $(function() { $("#dv_tools").append("<ul></ul>"); $.getJSON( "assets/json/tools.json", function( data ) { var tools = []; $.each( data.tools, function( key, val ) { tools[val["id"]]=val; }); $.each( data.audiences, function( key, val ) { // selector list $("<li><label><input type=radio name=audience value="+val["id"]+">"+val["name"]+"</label></li>").appendTo("#audience_list"); // selection content $("<li id=cat"+val["id"]+" class=vcat></li>").html(val["id"] + "::" + val["name"]).appendTo("#dv_tools ul"); }); }); $("input[name=audience]:radio").change(function () { $("#dv_tools ul").find('li').each(function() { if($(this).prop('id')=="cat"+$("input[name=audience]:checked").val()) $(this).show(); else $(this).hide(); alert("here!"); }); }); $('input[name=audience]:radio[value=2]').attr('checked', 'checked'); }); 
 <ul id="audience_list"></ul> ... <div id="dv_tools"></div> 

What it is suppose to do is to build a radio button selector list, after clicking on each item the content below should change dynamically. 假定要做的是建立一个单选按钮选择器列表,单击每个项目后,下面的内容应动态更改。

DOM is being built correctly: I see my list and content below, no problem. DOM构建正确:我在下面看到我的列表和内容,没问题。 But the event handler is not firing... 但是事件处理程序没有触发...

So this portion of the code doesn't work: 因此,这部分代码不起作用:

  $("input[name=audience]:radio").change(function () { $("#dv_tools ul").find('li').each(function() { if($(this).prop('id')=="cat"+$("input[name=audience]:checked").val()) $(this).show(); else $(this).hide(); alert("here!"); //not working :( }); }); $('input[name=audience]:radio[value=2]').attr('checked', 'checked'); 

It does work, however, when I have my list elements hardcoded in HTML directly. 但是,当我直接用HTML对列表元素进行硬编码时,它确实起作用。 I feel like I am missing something simple and it prob comes from my shallow understanding of jquery/js. 我觉得我缺少一些简单的东西,而它的出现可能是由于我对jquery / js的浅浅理解。

Thank you 谢谢

Adding json: 添加json:

{
"audiences": [
    {"id":"1","name":"Plan Sponsor (existing)","tools":"1,2,3,9"},
    {"id":"2","name":"Plan Sponsor (prospect)","tools":"4,6,2,3"},
    {"id":"3","name":"Professional Employer Organization (PEO) (existing)","tools":"1,2,3,7"},
    {"id":"4","name":"Professional Employer Organization (PEO) (prospect)","tools":"1,2,3,5"}        
],
"tools": [
    {"id":"1","name":"tool1","desc":"some description"},
    {"id":"2","name":"tool2","desc":"some description"},
    {"id":"3","name":"tool3","desc":"some description"},
    {"id":"4","name":"tool4","desc":"some description"},
    {"id":"5","name":"tool5","desc":"some description"}
]

} }

getJSON is asynchronous, which means that your suspicious code (starting with $("input[name=audience]:radio").change) is executed BEFORE the getJSON is completed, and therefore before your radio buttons were created, so they are not selected for your event listener. getJSON是异步的,这意味着您的可疑代码(以$(“ input [name = audience]:radio”:radio“开头).change是在getJSON完成之前执行的,因此在创建单选按钮之前,因此不会为您的事件监听器选择。

Try to move that code INSIDE the getJSON callback, right after creating the radio buttons, and it'll work like a charm! 尝试在创建单选按钮后立即将代码移到getJSON回调中,它将像超级按钮一样工作!

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

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