weixin_33713503 2010-08-19 12:03 采纳率: 0%
浏览 27

jQuery Ajax帮助

I working with some ajax at the moment, the result of a successful ajax result is that some more content is add to the page on the fly. My problem is that is added on the fly it looks like I cannot attach any events to the elements that are added.

The flow of what happens is that the user selects an option from a dropdown list, the value of that selection is sent to a php function which then returns some more HTML to the page which is appended to a div on the page.

I know there is a problem with the elements not existing on domReady as I run a length() check and it confirms they dont 'exist' on the page.

Is there away around this so that I can run click event on the HTML that gets added after the first ajax request has returned successfully.

$(document).ready(function() {

//customise the select menus
$('#customselector').SelectCustomizer();;

$('.career_select .selectitems').click(function(){
    var selectedCareer = $(this).attr('title');
    $.ajax({
        type: 'POST',
        url: '/roadmap/step_two',
        data: 'career_choice='+selectedCareer+"&ajax=true&submit_career=Next",
        success: function(html){
            $('.hfeed').append(html);
            $('#grade_choice').SelectCustomizer();
          }
    });
});

$('#grade_choice_options .selectitems').click(function(){
    var selectedGrade = $('#grade_choice_customselect').val();
    alert(selectedGrade);
})

});

  • 写回答

2条回答 默认 最新

  • elliott.david 2010-08-19 12:06
    关注

    Use live() instead of click() directly:

    $('.career_select .selectitems').live('click', function() { ....
    

    live() essentially wires up any new elements that match that are added subsequently.

    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗