如何在JavaScript中为HTML元素使用动态ID?

时间:2018-11-28 02:53:42

标签: javascript html dynamic

我试图动态创建下拉框,在这里我可以动态创建下拉框,但是此后,我需要从数据库加载值,因此需要执行点击功能。在这里,我尝试使用动态ID,但无法解决该问题。

$( document ).ready(function() {
    
    $("#ssr").click(function(){
        
      alert("Test");    
    
      load_shift_code();
    });
  
  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    for(var i=0;i<4;i++){
    var a=["html","css","java","javascript","php","c++","node.js","ASP","JSP","SQL"];
    var employee_name =emp_name();  
    var table_body = document.getElementById("table_head");
    var row = table_body.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2)
    var cell4 = row.insertCell(3);
    var cell5 = row.insertCell(4);
    var cell6 = row.insertCell(5)
    var cell7 = row.insertCell(6);
    var cell8 = row.insertCell(7);
    var cell9 = row.insertCell(8);
    var cell10 = row.insertCell(9);
    var cell11 = row.insertCell(10);
    var cell12 = row.insertCell(11);
    
    var drop_down_list =document.getElementById("select");
  var idx="ssr";
    cell1.innerHTML = employee_name[i];
    cell2.innerHTML = employee_name[i];
    cell3.innerHTML = "<select  id='" + idx +"'><option value='default'>default</option></select>";
    cell4.innerHTML = "<select  id='select'><option value='default'>default</option></select>";
    cell5.innerHTML = "<select  id='select'><option value='default'>default</option></select>";
    cell6.innerHTML = "<select  id='select'><option value='default'>default</option></select>";
    cell7.innerHTML = "<select  id='select'><option value='default'>default</option></select>";
    cell8.innerHTML = "<select  id='select'><option value='default'>default</option></select>";
    cell9.innerHTML = "<select  id='select'><option value='default'>default</option></select>";
    cell10.innerHTML ="<select  id='select'><option value='default'>default</option></select>";
    cell11.innerHTML ="<select  id='select'><option value='default'>default</option></select>";
    
}
  
  
  
});

1 个答案:

答案 0 :(得分:0)

在创建元素并将其放入DOM的代码之后,放置将事件侦听器添加到ID为class MyForm(forms.ModelForm): ... class Meta: model = Contact fields = ('field_1', 'field_2', 'field_3', 'field_4',) 的元素的代码。 ssr如前所述,找不到任何内容。