2013-05-28 22 views
1

嘿,我試圖添加第二個按鈕到這個Apprise V2 JS代碼。但是,我似乎無法獲得正確的點擊事件,無論點擊哪一個。Jquery apprise v2 2個按鈕

添加代碼的按鍵部分:

// Add buttons 
$.each(settings.buttons, function(i, button) { 

    if(button) {    
     button.id = "Accept"; 
     button.text = "Accept"; 

     var $_button = $('<button id="apprise-btn-' + button.id + '">').append(button.text); 

     button.className = "green"; 
     $_button.addClass(button.className); 
     $_buttons.append($_button);   
     $_button.on("click", function() { 
      var response = { 
       clicked: button, 
      }; 

      button.action(response); 
     }); 

     // Deny button 
     button.id = "Deny"; 
     button.text = "Deny"; 

     var $_button = $('<button id="apprise-btn-' + button.id + '">').append(button.text); 

     button.className = "red"; 
     $_button.addClass(button.className); 
     $_buttons.append($_button); 
     $_button.on("click", function() { 
      var response = { 
       clicked: button, 
      }; 

      button.action(response); 
     }); 
    } 
}); 

這是觸發該框代碼的一部分:

$(function() { 
    $('#custom-tryit').on("click", function() {  
     var $response = $('custom-response');  
     var options = { 
      buttons: { 
       confirm: { 
        text: 'Accept', 
        className: 'blue', 
        action: function(e) { 
        console.log(this.id); 

         if (this.id == 'Deny') { 
          //Goto home page 
          console.log('home page'); 
         } else { 
          //carry on 
          console.log('carry on'); 
         } 

         Apprise('close'); 
        } 
       }, 
      }, 
      input: false 
     }; 

     Apprise('This is just a test here', options); 
    }); 
}); 

似乎原代碼並沒有有一個選項對於多於1個按鈕...輸出似乎是輸出兩個值而不是隻是點擊了什麼(console.log(this.id);):

Deny 
home page 

任何幫助都會很棒!

JSFIDDLE

回答

1

我修改從響應:

$_button.on("click", function() { 
      var response = { 
       clicked: button, 
      }; 

$_button.on("click", function() { 
      var response = { 
       clicked: "Accept", 
      }; 

,做爲拒絕按鈕是相同的。

然後在

$('#custom-tryit') 

的單擊事件我用

if (e.clicked == 'Deny') { 

,而不是this.id,它似乎很好地工作。檢查出來:

http://jsfiddle.net/b2YZH/

+0

太棒了。這工作!感謝那裏的幫助,Sanpopo – StealthRT