2016-06-08 52 views
0

我有一個單選按鈕,並以編程方式向它添加元素。正如你所看到的,我在html代碼中設置了onclick事件,但是當我推送這個項目時它不會被觸發。以編程方式在javascript和onclick事件中添加單選項

<div class="btn-group" data-toggle="buttons" name="radio" id="selection"> 
</div> 

<script type="text/javascript"> 
    var selection = document.getElementById("selection"); 
    selection.style.visibility = 'visible'; 
    var selectionToAdd=''; 
    var toAdd=[]; 
    for(var indexSelectionArticles = 0 in query) { 
     for(var indexSelectionEntities = 0 in query[indexSelectionArticles].entities) { 
      if(query[indexSelectionArticles].entities[indexSelectionEntities].category){ 
       if($.inArray(query[indexSelectionArticles].entities[indexSelectionEntities].category, toAdd) == -1) { 
        selectionToAdd=selectionToAdd + '<label class="btn btn-default"><input type="radio" onclick="javascript:console.log(whatever);" name="options" id="option'+query[indexSelectionArticles].entities[indexSelectionEntities].category+'">&nbsp; <i class="fa fa-'+query[indexSelectionArticles].entities[indexSelectionEntities].category+'"></i>&nbsp; '+capitalizeFirstLetter(query[indexSelectionArticles].entities[indexSelectionEntities].category)+' &nbsp;</input></label>'; 
        toAdd.push(query[indexSelectionArticles].entities[indexSelectionEntities].category); 
       } 
      } 
     } 
    } 
    selection.innerHTML = selectionToAdd; 
</script> 

該項目已正確添加,但未觸發onclick。我也嘗試過一個功能,但結果是一樣的。我也試過聽者如下,這與網頁中的其他無線電工作,但不是這一個:

$('input[type="radio"]').on('click change', function(e) { 

    if(e.target.id == 'option1') { 
     option = 'option1'; 
    } 
    else if(e.target.id == 'option2') { 
     option = 'option2'; 
    } 
    else if(e.target.id == 'option3') { 
     option = 'option3'; 
    } 
    else if(e.target.id == 'option4') { 
     option = 'option4'; 
    } 
    else if(e.target.id == 'option5') { 
     visualizationType = 'articles'; 
    } 
    else if(e.target.id == 'option6') { 
     visualizationType = 'entities'; 
    } 
    else { 
     console.log(e.target.id); 
    } 
}); 

哪裏錯誤?

謝謝

回答

1

你在<label>包裹<input>。 onclick事件不需要在<label>而不是<input>上?

0

它看起來像你沒有初始化你的腳本中的'查詢'變量。

相關問題