2013-06-04 30 views
4

我的页面有三个html选择标记。jQuery的更改()不起作用

我想这三个选择标签具有以下功能:

当我改变SELECTA,不是根据SELECTA selectB自动变化。 当selectB的选项已经创建,当我改变selectB比selectC自动改变selectB。

例如...

第一SELECTA的选项有歌星,影星,selectB只是空的。

当歌手选项选中,selectB自动创建选项 “Lady Gaga的”,“ Celine Dion“,”惠特尼休斯顿“,如果三位歌手创作过,当我选中”Lady Gaga“时,selectC将创建”生日“,”Facebook“,”google +“选项

当检查影片星标选项时, selectB自动创建选项“Bruce Wi llis“,”马特达蒙“,”威尔史密斯“,如果三个电影明星创建,当我选中”布鲁斯威利斯“时,selectC将创建”生日“,”Facebook“,”谷歌+“选项。

我的问题是...当我改变selectB,$( “#selectB_id”)变化()无法正常工作

以下是我的javascript代码:

$(function(){ 
$("#lineselect").change(function() { 
    $.ajax({ 
     url: '/lineflow/ajaxgetselect', 
     type:'POST', 
     data:{ service_id:$("#lineselect option:checked").val() , level:1 }, 
     success: function(res){ 
      var obj = jQuery.parseJSON(res); 

      if($("#lineselect").val() == 0) 
      { 
       $("#second_li").html(""); 
       $("#third_li").html(""); 
      } 
      else if($("#lineselect").val() == 1) 
      { 
       $("#second_li").html(""); 
       $("#third_li").html(""); 
       $("#second_li").html('<select id="service_type" name="service_type"><option value="0">ALL</option></select>'); 
       $("#third_li").html('<select id="service_name" name="service_name"><option value="0">ALL</option></select>'); 
       for(i=0; i<obj.length; i++) 
       { 
        $('#service_type').append($("<option></option>") 
            .attr("value",obj[i].id) 
            .text(obj[i].name)); 
       } 
      } 
      else if($("#lineselect").val() == 2) 
      { 
       $("#second_li").html(""); 
       $("#third_li").html(""); 
       $("#second_li").html('<input type="text" id="url_name" name="url_name"></input>'); 
      } 
      else if($("#lineselect").val() == 3) 
      { 
       $("#second_li").html(""); 
       $("#third_li").html(""); 
       $("#second_li").html('<select id="url_type" name="url_type"><option value="0">ALL</option></select>'); 
       for(i=0; i<obj.length; i++) 
       { 
        $('#url_type').append($("<option></option>") 
            .attr("value",obj[i].id) 
            .text(obj[i].name)); 
       } 
      } 
     }, 
     error: function(obj, status, err){} 

    }); 

}); 

$("#service_type").change(function() {   // this change not work!!! 
    $.ajax({ 
     url: '/lineflow/ajaxgetselect', 
     type:'POST', 
     data:{ service_id:$("#service_type option:checked").val() , level:2 }, 
     success: function(res){ 
      var obj = jQuery.parseJSON(res);    

      $("#third_li").html(""); 
      $("#third_li").html('<select id="service_name" name="service_name"><option value="0">ALL</option></select>'); 
      for(i=0; i<obj.length; i++) 
      { 
       $('#service_type').append($("<option></option>") 
           .attr("value",obj[i].id) 
           .text(obj[i].name)); 
      } 

     }, 
     error: function(obj, status, err){} 

    }); 

}); 

});

+0

试试 “上” 而不是 “变化”:$(...)在( '变', ...) – Atber

+1

@Aber没有区别......'.change(func)'只是'.on(“change”,func)的快捷方式' – Ian

回答

4

在此行中:

$("#second_li").html('<select id="service_type" name="service_type"><option value="0">ALL</option></select>'); 

正如你看到的,你是#service_type元素添加到DOM,后附加事件处理它。 因此,您必须使用事件委派。这可以通过.on()方法的jQuery来完成:

$('#second_li').on('change', '#service_type', function() { 
    $.ajax({ 
     // 
    }); 
}); 
  • 请注意,您的#second_li元素必须是静态的页面上。否则使用其他选择器。

参考文献:

  • .on() - jQuery的API文档
+0

哦!这么。我必须学习活动代表团的一部分,非常感谢你! – user1770147

4

使用事件代表团:

$("body").on('change', '#service_type', function() { 
    // your code 
}); 
+0

谢谢你帮我处理这个问题^^ – user1770147