2013-03-15 86 views
2

我* .aspx頁面中(主頁):局部視圖點擊事件不火

function getEmployeeHours(employeeId,year,month) { 
$('#clocked-details').load('/Employees/GetEmployeeClockedHours/',{ 'employeeId': employeeId,'year': year,'month': month }); 
    }; 

我的局部視圖*的.ascx:

<td> 
    <button id="btnSave" type="button" class="actionButton"> 
    Save</button> 
    </td> 

正如上面的代碼片段,我需要_1的存取局部視圖btnSave從主視圖觸發點擊事件。

我在主視圖裏面寫了下面的代碼。

$('#btnSave').off('click').on('click', function() { 

     var yearValue = $("#year").val(); 
     var monthValue = $("#month").val(); 

     $.ajax({ 
      url: "/Employees/UpdateEmployeeClockedHoursByProvider", 
      type: 'POST', 
      cache: false, 
      data: { employeeId: employeeId, year: yearValue, month: monthValue }, 
      success: function (result) { 

      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       alert(xhr.status); 
       alert(thrownError); 
      } 
     }); 

     return false; 
    }); 

不過,這並不fire.Here我使用負載 jQuery的方法異步加載我的部分觀點。

所以我的問題是我應該如何點擊部分視圖的點擊事件?

+0

部分視圖或主視圖中的按鈕點擊事件? – 2013-03-15 16:24:02

+0

@SivaGopal我把這個事件放在兩個地方。但沒有運氣?任何想法? – Sampath 2013-03-15 16:25:21

+0

您是否嘗試過(「#btnSave」),jquery的live(「click」)事件,同時將腳本保留在主視圖中? – 2013-03-15 16:26:38

回答

1

由於用戶控件containging按鈕被動態加載,可以同時保持腳本在主視圖,可跟蹤控件被添加到未來的DOM使用("#btnSave").live("click"){...}事件的jQuery。因此,無論何時只要出現與「live(..)」的指定選擇器匹配的控件,該事件就會自動進行佈線。

希望這對你有所幫助。

+0

感謝您的回答。 – Sampath 2013-03-20 21:05:16

6

由於您的html正在動態加載到頁面中,因此您必須爲您的DOM查詢提供更多上下文。

試試這個。

$(document).on('click', '#bntSave', function(){ 
    //do stuff 
});