2014-03-03 74 views
-1

這裏我有創建動態按鈕的代碼。click事件有問題。動態顯示JQuery按鈕

在那裏我有

alert("work"); 

開始與此警報線之上,代碼是行不通的。任何幫助?

更新3

// Creating a checkbox for each category and place it in container: 

    for (var i in chart.xAxis[0].categories) { 

     checkboxHtml = '<label><button type ="submit" id="category' + i + '" name="category' + i + '">' + i + '</button></label>' 

     $('#' + legendContainerId).append(checkboxHtml); 

    } 

    // Binding events to checkboxes: 

    $('#' + legendContainerId + ' button[type ="submit"]').on('click', function() { 


     var hiddenSeries = []; 

     var hiddenCategories = []; 


     // Collecting disabled categories: 

     $('#' + legendContainerId + 'button[type ="submit"]').each(function (i, submit) { 


      alert("work"); 
      if (!submit.onselect) { 
       hiddenSeries[i] = null; 

       hiddenCategories[i] = ' '; 

      } 
+1

你嘗試過type = button嗎? –

+0

是的,但如何更改checkboxHtml? – user1476956

+0

checkboxHtml?請解釋一下。 – tymeJV

回答

0

我真的不知道你想要什麼,但如果你正在尋找的是改變checkboxesbuttons,那麼你只需要改變你的HTML字符串邏輯。例如:

var html = '<label><button id="category' + i + '" name="category' + i + '">' + i + '</button></label>' 

將輸出:

<label><button id="categoryX" name="categoryX">X</button></label> 

哪裏X是從for循環的動態值。

Here's an example

+0

亞現在我可以看到按鈕,但仍然無法工作。我添加一些其他代碼,你可以看到問題。 – user1476956

+0

@ user1476956什麼不工作?! – Charlie

+0

不可點擊。 – user1476956

0

你試圖更改以下行:

$('#' + legendContainerId + 'button[type ="submit"]' + i + '"').each(function (i, submit) { 

要:

$('#' + legendContainerId + ' button[type ="submit"]').each(function (i, submit) { 

-One空間字符之前 「按鈕[...」 和 「i」 變種刪除

+0

是我修復「我「。謝謝你的空間字符也是問題。現在我收到警報。保留下面的代碼如何檢查按鈕是否點擊 – user1476956

0

對於動態創建的按鈕,適用於我

$(document).on('click', 'classOfButton', function (t) { 
    var me= $(this).text(); 
    alert(me); 
    }) 
+0

我怎樣才能在我的例子中使用?\ – user1476956

+0

使用您的$ click事件)。上() –